StringHelper类的代码也写得不错,值得好好学习学习

本文介绍了一个实用的StringHelper类,该类提供了多种字符串处理方法,包括编码与解码、替换、过滤敏感词、HTML编码与解码等功能。通过这些方法可以方便地实现字符串的各种操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加了些注释,不知道加得对不对,如果不对,请过客告诉我,谢谢.
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  System.Web;
None.gif
using  System.Security.Cryptography;
None.gif
None.gif
namespace  Cvv.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static class StringHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
静态方法#region 静态方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对字符串进行base64编码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>base64编码串</returns>

InBlock.gif        public static string Base64StringEncode(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input);
InBlock.gif            
return Convert.ToBase64String(encbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对字符串进行反编码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">base64编码串</param>
ExpandedSubBlockEnd.gif        
/// <returns>字符串</returns>

InBlock.gif        public static string Base64StringDecode(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] decbuff = Convert.FromBase64String(input);
InBlock.gif            
return System.Text.Encoding.UTF8.GetString(decbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 替换字符串(忽略大小写)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行替换的内容</param>
InBlock.gif        
/// <param name="oldValue">旧字符串</param>
InBlock.gif        
/// <param name="newValue">新字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>替换后的字符串</returns>

InBlock.gif        public static string CaseInsensitiveReplace(string input, string oldValue, string newValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex regEx 
= new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline);
InBlock.gif            
return regEx.Replace(input, newValue);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 替换首次出现的字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行替换的内容</param>
InBlock.gif        
/// <param name="oldValue">旧字符串</param>
InBlock.gif        
/// <param name="newValue">新字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>替换后的字符串</returns>

InBlock.gif        public static string ReplaceFirst(string input, string oldValue, string newValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex regEx 
= new Regex(oldValue, RegexOptions.Multiline);
InBlock.gif            
return regEx.Replace(input, newValue, 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 替换最后一次出现的字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行替换的内容</param>
InBlock.gif        
/// <param name="oldValue">旧字符串</param>
InBlock.gif        
/// <param name="newValue">新字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>替换后的字符串</returns>

InBlock.gif        public static string ReplaceLast(string input, string oldValue, string newValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int index = input.LastIndexOf(oldValue);
InBlock.gif            
if (index < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return input;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringBuilder sb 
= new StringBuilder(input.Length - oldValue.Length + newValue.Length);
InBlock.gif                sb.Append(input.Substring(
0, index));
InBlock.gif                sb.Append(newValue);
InBlock.gif                sb.Append(input.Substring(index 
+ oldValue.Length, input.Length - index - oldValue.Length));
InBlock.gif                
return sb.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据词组过虑字符串(忽略大小写)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行过虑的内容</param>
InBlock.gif        
/// <param name="filterWords">要过虑的词组</param>
ExpandedSubBlockEnd.gif        
/// <returns>过虑后的字符串</returns>

InBlock.gif        public static string FilterWords(string input, params string[] filterWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.FilterWords(input, char.MinValue, filterWords);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据词组过虑字符串(忽略大小写)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行过虑的内容</param>
InBlock.gif        
/// <param name="mask">字符掩码</param>
InBlock.gif        
/// <param name="filterWords">要过虑的词组</param>
ExpandedSubBlockEnd.gif        
/// <returns>过虑后的字符串</returns>

InBlock.gif        public static string FilterWords(string input, char mask, params string[] filterWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string stringMask = mask == char.MinValue ? string.Empty : mask.ToString();
InBlock.gif            
string totalMask = stringMask;
InBlock.gif
InBlock.gif            
foreach (string s in filterWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Regex regEx 
= new Regex(s, RegexOptions.IgnoreCase | RegexOptions.Multiline);
InBlock.gif
InBlock.gif                
if (stringMask.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int i = 1; i < s.Length; i++)
InBlock.gif                        totalMask 
+= stringMask;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                input 
= regEx.Replace(input, totalMask);
InBlock.gif
InBlock.gif                totalMask 
= stringMask;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return input;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static MatchCollection HasWords(string input, params string[] hasWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder sb 
= new StringBuilder(hasWords.Length + 50);
InBlock.gif            
//sb.Append("[");
InBlock.gif

InBlock.gif            
foreach (string s in hasWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.AppendFormat(
"({0})|", StringHelper.HtmlSpecialEntitiesEncode(s.Trim()));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string pattern = sb.ToString();
InBlock.gif            pattern 
= pattern.TrimEnd('|'); // +"]";
InBlock.gif

InBlock.gif            Regex regEx 
= new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
InBlock.gif            
return regEx.Matches(input);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Html编码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行编辑的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>Html编码后的字符串</returns>

InBlock.gif        public static string HtmlSpecialEntitiesEncode(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return HttpUtility.HtmlEncode(input);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Html解码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行解码的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>解码后的字符串</returns>

InBlock.gif        public static string HtmlSpecialEntitiesDecode(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return HttpUtility.HtmlDecode(input);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// MD5加密
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行加密的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>加密后的字符串</returns>

InBlock.gif        public static string MD5String(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MD5 md5Hasher 
= MD5.Create();
InBlock.gif
InBlock.gif            
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
InBlock.gif
InBlock.gif            StringBuilder sBuilder 
= new StringBuilder();
InBlock.gif
InBlock.gif            
for (int i = 0; i < data.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sBuilder.Append(data[i].ToString(
"x2"));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sBuilder.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对字符串进行MD5较验
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行较验的字符串</param>
InBlock.gif        
/// <param name="hash">散列串</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否匹配</returns>

InBlock.gif        public static bool MD5VerifyString(string input, string hash)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string hashOfInput = StringHelper.MD5String(input);
InBlock.gif
InBlock.gif            StringComparer comparer 
= StringComparer.OrdinalIgnoreCase;
InBlock.gif
InBlock.gif            
if (0 == comparer.Compare(hashOfInput, hash))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadLeftHtmlSpaces(string input, int totalSpaces)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string space = "&nbsp;";
InBlock.gif            
return PadLeft(input, space, totalSpaces * space.Length);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadLeft(string input, string pad, int totalWidth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.PadLeft(input, pad, totalWidth, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadLeft(string input, string pad, int totalWidth, bool cutOff)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (input.Length >= totalWidth)
InBlock.gif                
return input;
InBlock.gif
InBlock.gif            
int padCount = pad.Length;
InBlock.gif            
string paddedString = input;
InBlock.gif
InBlock.gif            
while (paddedString.Length < totalWidth)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                paddedString 
+= pad;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// trim the excess.
InBlock.gif
            if (cutOff)
InBlock.gif                paddedString 
= paddedString.Substring(0, totalWidth);
InBlock.gif
InBlock.gif            
return paddedString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadRightHtmlSpaces(string input, int totalSpaces)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string space = "&nbsp;";
InBlock.gif            
return PadRight(input, space, totalSpaces * space.Length);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadRight(string input, string pad, int totalWidth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.PadRight(input, pad, totalWidth, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string PadRight(string input, string pad, int totalWidth, bool cutOff)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (input.Length >= totalWidth)
InBlock.gif                
return input;
InBlock.gif
InBlock.gif            
string paddedString = string.Empty;
InBlock.gif
InBlock.gif            
while (paddedString.Length < totalWidth - input.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                paddedString 
+= pad;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// trim the excess.
InBlock.gif
            if (cutOff)
InBlock.gif                paddedString 
= paddedString.Substring(0, totalWidth - input.Length);
InBlock.gif
InBlock.gif            paddedString 
+= input;
InBlock.gif
InBlock.gif            
return paddedString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 去除新行
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要去除新行的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>已经去除新行的字符串</returns>

InBlock.gif        public static string RemoveNewLines(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.RemoveNewLines(input, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 去除新行
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要去除新行的字符串</param>
InBlock.gif        
/// <param name="addSpace">是否添加空格</param>
ExpandedSubBlockEnd.gif        
/// <returns>已经去除新行的字符串</returns>

InBlock.gif        public static string RemoveNewLines(string input, bool addSpace)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string replace = string.Empty;
InBlock.gif            
if (addSpace)
InBlock.gif                replace 
= " ";
InBlock.gif
InBlock.gif            
string pattern = @"[\r|\n]";
InBlock.gif            Regex regEx 
= new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
InBlock.gif
InBlock.gif            
return regEx.Replace(input, replace);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 字符串反转
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行反转的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>反转后的字符串</returns>

InBlock.gif        public static string Reverse(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char[] reverse = new char[input.Length];
InBlock.gif            
for (int i = 0, k = input.Length - 1; i < input.Length; i++, k--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (char.IsSurrogate(input[k]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    reverse[i 
+ 1= input[k--];
InBlock.gif                    reverse[i
++= input[k];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    reverse[i] 
= input[k];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return new System.String(reverse);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 转成首字母大字形式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行转换的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>转换后的字符串</returns>

InBlock.gif        public static string SentenceCase(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (input.Length < 1)
InBlock.gif                
return input;
InBlock.gif
InBlock.gif            
string sentence = input.ToLower();
InBlock.gif            
return sentence[0].ToString().ToUpper() + sentence.Substring(1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 空格转换成&nbsp;
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行转换的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>转换后的字符串</returns>

InBlock.gif        public static string SpaceToNbsp(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string space = "&nbsp;";
InBlock.gif            
return input.Replace(" ", space);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 去除"<" 和 ">" 符号之间的内容
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string StripTags(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex stripTags 
= new Regex("<(.|\n)+?>");
InBlock.gif            
return stripTags.Replace(input, "");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string TitleCase(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return TitleCase(input, true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string TitleCase(string input, bool ignoreShortWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List
<string> ignoreWords = null;
InBlock.gif            
if (ignoreShortWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//TODO: Add more ignore words?
InBlock.gif
                ignoreWords = new List<string>();
InBlock.gif                ignoreWords.Add(
"a");
InBlock.gif                ignoreWords.Add(
"is");
InBlock.gif                ignoreWords.Add(
"was");
InBlock.gif                ignoreWords.Add(
"the");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string[] tokens = input.Split(' ');
InBlock.gif            StringBuilder sb 
= new StringBuilder(input.Length);
InBlock.gif            
foreach (string s in tokens)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ignoreShortWords == true
InBlock.gif                    
&& s != tokens[0]
InBlock.gif                    
&& ignoreWords.Contains(s.ToLower()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sb.Append(s 
+ " ");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sb.Append(s[
0].ToString().ToUpper());
InBlock.gif                    sb.Append(s.Substring(
1).ToLower());
InBlock.gif                    sb.Append(
" ");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sb.ToString().Trim();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 去除字符串内的空白字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string TrimIntraWords(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex regEx 
= new Regex(@"[\s]+");
InBlock.gif            
return regEx.Replace(input, " ");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 换行符转换成Html标签的换行符<br />
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string NewLineToBreak(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex regEx 
= new Regex(@"[\n|\r]+");
InBlock.gif            
return regEx.Replace(input, "<br />");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入换行符(不中断单词)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
InBlock.gif        
/// <param name="charCount">每行字符数</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string WordWrap(string input, int charCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.WordWrap(input, charCount, false, Environment.NewLine);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入换行符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
InBlock.gif        
/// <param name="charCount">每行字符数</param>
InBlock.gif        
/// <param name="cutOff">如果为真,将在单词的中部断开</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string WordWrap(string input, int charCount, bool cutOff)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return StringHelper.WordWrap(input, charCount, cutOff, Environment.NewLine);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入换行符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="input">要进行处理的字符串</param>
InBlock.gif        
/// <param name="charCount">每行字符数</param>
InBlock.gif        
/// <param name="cutOff">如果为真,将在单词的中部断开</param>
InBlock.gif        
/// <param name="breakText">插入的换行符号</param>
ExpandedSubBlockEnd.gif        
/// <returns>处理后的字符串</returns>

InBlock.gif        public static string WordWrap(string input, int charCount, bool cutOff, string breakText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder sb 
= new StringBuilder(input.Length + 100);
InBlock.gif            
int counter = 0;
InBlock.gif
InBlock.gif            
if (cutOff)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while (counter < input.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (input.Length > counter + charCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(input.Substring(counter, charCount));
InBlock.gif                        sb.Append(breakText);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(input.Substring(counter));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    counter 
+= charCount;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] strings = input.Split(' ');
InBlock.gif                
for (int i = 0; i < strings.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    counter 
+= strings[i].Length + 1// the added one is to represent the inclusion of the space.
InBlock.gif
                    if (i != 0 && counter > charCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(breakText);
InBlock.gif                        counter 
= 0;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    sb.Append(strings[i] 
+ ' ');
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sb.ToString().TrimEnd(); // to get rid of the extra space at the end.
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
posted on 2007-04-29 22:58 黄尚 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/afxcn/archive/2007/04/29/732842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值