[转]StringHelper类

点我,点我,点我
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Text.RegularExpressions;
  5 using System.Web;
  6 using System.Security.Cryptography;
  7 
  8 namespace Cvv.Components
  9 {
 10     public static class StringHelper
 11     {
 12         静态方法#region 静态方法
 13         /**//// <summary>
 14         /// 对字符串进行base64编码
 15         /// </summary>
 16         /// <param name="input">字符串</param>
 17         /// <returns>base64编码串</returns>
 18         public static string Base64StringEncode(string input)
 19         {
 20             byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input);
 21             return Convert.ToBase64String(encbuff);
 22         }
 23 
 24         /**//// <summary>
 25         /// 对字符串进行反编码
 26         /// </summary>
 27         /// <param name="input">base64编码串</param>
 28         /// <returns>字符串</returns>
 29         public static string Base64StringDecode(string input)
 30         {
 31             byte[] decbuff = Convert.FromBase64String(input);
 32             return System.Text.Encoding.UTF8.GetString(decbuff);
 33         }
 34 
 35         /**//// <summary>
 36         /// 替换字符串(忽略大小写)
 37         /// </summary>
 38         /// <param name="input">要进行替换的内容</param>
 39         /// <param name="oldValue">旧字符串</param>
 40         /// <param name="newValue">新字符串</param>
 41         /// <returns>替换后的字符串</returns>
 42         public static string CaseInsensitiveReplace(string input, string oldValue, string newValue)
 43         {
 44             Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline);
 45             return regEx.Replace(input, newValue);
 46         }
 47 
 48         /**//// <summary>
 49         /// 替换首次出现的字符串
 50         /// </summary>
 51         /// <param name="input">要进行替换的内容</param>
 52         /// <param name="oldValue">旧字符串</param>
 53         /// <param name="newValue">新字符串</param>
 54         /// <returns>替换后的字符串</returns>
 55         public static string ReplaceFirst(string input, string oldValue, string newValue)
 56         {
 57             Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
 58             return regEx.Replace(input, newValue, 1);
 59         }
 60 
 61         /**//// <summary>
 62         /// 替换最后一次出现的字符串
 63         /// </summary>
 64         /// <param name="input">要进行替换的内容</param>
 65         /// <param name="oldValue">旧字符串</param>
 66         /// <param name="newValue">新字符串</param>
 67         /// <returns>替换后的字符串</returns>
 68         public static string ReplaceLast(string input, string oldValue, string newValue)
 69         {
 70             int index = input.LastIndexOf(oldValue);
 71             if (index < 0)
 72             {
 73                 return input;
 74             }
 75             else
 76             {
 77                 StringBuilder sb = new StringBuilder(input.Length - oldValue.Length + newValue.Length);
 78                 sb.Append(input.Substring(0, index));
 79                 sb.Append(newValue);
 80                 sb.Append(input.Substring(index + oldValue.Length, input.Length - index - oldValue.Length));
 81                 return sb.ToString();
 82             }
 83         }
 84 
 85         /**//// <summary>
 86         /// 根据词组过虑字符串(忽略大小写)
 87         /// </summary>
 88         /// <param name="input">要进行过虑的内容</param>
 89         /// <param name="filterWords">要过虑的词组</param>
 90         /// <returns>过虑后的字符串</returns>
 91         public static string FilterWords(string input, params string[] filterWords)
 92         {
 93             return StringHelper.FilterWords(input, char.MinValue, filterWords);
 94         }
 95 
 96         /**//// <summary>
 97         /// 根据词组过虑字符串(忽略大小写)
 98         /// </summary>
 99         /// <param name="input">要进行过虑的内容</param>
100         /// <param name="mask">字符掩码</param>
101         /// <param name="filterWords">要过虑的词组</param>
102         /// <returns>过虑后的字符串</returns>
103         public static string FilterWords(string input, char mask, params string[] filterWords)
104         {
105             string stringMask = mask == char.MinValue ? string.Empty : mask.ToString();
106             string totalMask = stringMask;
107 
108             foreach (string s in filterWords)
109             {
110                 Regex regEx = new Regex(s, RegexOptions.IgnoreCase | RegexOptions.Multiline);
111 
112                 if (stringMask.Length > 0)
113                 {
114                     for (int i = 1; i < s.Length; i++)
115                         totalMask += stringMask;
116                 }
117 
118                 input = regEx.Replace(input, totalMask);
119 
120                 totalMask = stringMask;
121             }
122 
123             return input;
124         }
125 
126         public static MatchCollection HasWords(string input, params string[] hasWords)
127         {
128             StringBuilder sb = new StringBuilder(hasWords.Length + 50);
129             //sb.Append("[");
130 
131             foreach (string s in hasWords)
132             {
133                 sb.AppendFormat("({0})|", StringHelper.HtmlSpecialEntitiesEncode(s.Trim()));
134             }
135 
136             string pattern = sb.ToString();
137             pattern = pattern.TrimEnd('|'); // +"]";
138 
139             Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
140             return regEx.Matches(input);
141         }
142 
143         /**//// <summary>
144         /// Html编码
145         /// </summary>
146         /// <param name="input">要进行编辑的字符串</param>
147         /// <returns>Html编码后的字符串</returns>
148         public static string HtmlSpecialEntitiesEncode(string input)
149         {
150             return HttpUtility.HtmlEncode(input);
151         }
152 
153         /**//// <summary>
154         /// Html解码
155         /// </summary>
156         /// <param name="input">要进行解码的字符串</param>
157         /// <returns>解码后的字符串</returns>
158         public static string HtmlSpecialEntitiesDecode(string input)
159         {
160             return HttpUtility.HtmlDecode(input);
161         }
162 
163         /**//// <summary>
164         /// MD5加密
165         /// </summary>
166         /// <param name="input">要进行加密的字符串</param>
167         /// <returns>加密后的字符串</returns>
168         public static string MD5String(string input)
169         {
170             MD5 md5Hasher = MD5.Create();
171 
172             byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
173 
174             StringBuilder sBuilder = new StringBuilder();
175 
176             for (int i = 0; i < data.Length; i++)
177             {
178                 sBuilder.Append(data[i].ToString("x2"));
179             }
180 
181             return sBuilder.ToString();
182         }
183 
184         /**//// <summary>
185         /// 对字符串进行MD5较验
186         /// </summary>
187         /// <param name="input">要进行较验的字符串</param>
188         /// <param name="hash">散列串</param>
189         /// <returns>是否匹配</returns>
190         public static bool MD5VerifyString(string input, string hash)
191         {
192             string hashOfInput = StringHelper.MD5String(input);
193 
194             StringComparer comparer = StringComparer.OrdinalIgnoreCase;
195 
196             if (0 == comparer.Compare(hashOfInput, hash))
197             {
198                 return true;
199             }
200             else
201             {
202                 return false;
203             }
204         }
205 
206         public static string PadLeftHtmlSpaces(string input, int totalSpaces)
207         {
208             string space = "&nbsp;";
209             return PadLeft(input, space, totalSpaces * space.Length);
210         }
211 
212         public static string PadLeft(string input, string pad, int totalWidth)
213         {
214             return StringHelper.PadLeft(input, pad, totalWidth, false);
215         }
216 
217         public static string PadLeft(string input, string pad, int totalWidth, bool cutOff)
218         {
219             if (input.Length >= totalWidth)
220                 return input;
221 
222             int padCount = pad.Length;
223             string paddedString = input;
224 
225             while (paddedString.Length < totalWidth)
226             {
227                 paddedString += pad;
228             }
229 
230             // trim the excess.
231             if (cutOff)
232                 paddedString = paddedString.Substring(0, totalWidth);
233 
234             return paddedString;
235         }
236 
237         public static string PadRightHtmlSpaces(string input, int totalSpaces)
238         {
239             string space = "&nbsp;";
240             return PadRight(input, space, totalSpaces * space.Length);
241         }
242 
243         public static string PadRight(string input, string pad, int totalWidth)
244         {
245             return StringHelper.PadRight(input, pad, totalWidth, false);
246         }
247 
248         public static string PadRight(string input, string pad, int totalWidth, bool cutOff)
249         {
250             if (input.Length >= totalWidth)
251                 return input;
252 
253             string paddedString = string.Empty;
254 
255             while (paddedString.Length < totalWidth - input.Length)
256             {
257                 paddedString += pad;
258             }
259 
260             // trim the excess.
261             if (cutOff)
262                 paddedString = paddedString.Substring(0, totalWidth - input.Length);
263 
264             paddedString += input;
265 
266             return paddedString;
267         }
268 
269         /**//// <summary>
270         /// 去除新行
271         /// </summary>
272         /// <param name="input">要去除新行的字符串</param>
273         /// <returns>已经去除新行的字符串</returns>
274         public static string RemoveNewLines(string input)
275         {
276             return StringHelper.RemoveNewLines(input, false);
277         }
278 
279         /**//// <summary>
280         /// 去除新行
281         /// </summary>
282         /// <param name="input">要去除新行的字符串</param>
283         /// <param name="addSpace">是否添加空格</param>
284         /// <returns>已经去除新行的字符串</returns>
285         public static string RemoveNewLines(string input, bool addSpace)
286         {
287             string replace = string.Empty;
288             if (addSpace)
289                 replace = " ";
290 
291             string pattern = @"[\r|\n]";
292             Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
293 
294             return regEx.Replace(input, replace);
295         }
296 
297         /**//// <summary>
298         /// 字符串反转
299         /// </summary>
300         /// <param name="input">要进行反转的字符串</param>
301         /// <returns>反转后的字符串</returns>
302         public static string Reverse(string input)
303         {
304             char[] reverse = new char[input.Length];
305             for (int i = 0, k = input.Length - 1; i < input.Length; i++, k--)
306             {
307                 if (char.IsSurrogate(input[k]))
308                 {
309                     reverse[i + 1] = input[k--];
310                     reverse[i++] = input[k];
311                 }
312                 else
313                 {
314                     reverse[i] = input[k];
315                 }
316             }
317             return new System.String(reverse);
318         }
319 
320         /**//// <summary>
321         /// 转成首字母大字形式
322         /// </summary>
323         /// <param name="input">要进行转换的字符串</param>
324         /// <returns>转换后的字符串</returns>
325         public static string SentenceCase(string input)
326         {
327             if (input.Length < 1)
328                 return input;
329 
330             string sentence = input.ToLower();
331             return sentence[0].ToString().ToUpper() + sentence.Substring(1);
332         }
333 
334         /**//// <summary>
335         /// 空格转换成&nbsp;
336         /// </summary>
337         /// <param name="input">要进行转换的字符串</param>
338         /// <returns>转换后的字符串</returns>
339         public static string SpaceToNbsp(string input)
340         {
341             string space = "&nbsp;";
342             return input.Replace(" ", space);
343         }
344 
345         /**//// <summary>
346         /// 去除"<" 和 ">" 符号之间的内容
347         /// </summary>
348         /// <param name="input">要进行处理的字符串</param>
349         /// <returns>处理后的字符串</returns>
350         public static string StripTags(string input)
351         {
352             Regex stripTags = new Regex("<(.|\n)+?>");
353             return stripTags.Replace(input, "");
354         }
355 
356         public static string TitleCase(string input)
357         {
358             return TitleCase(input, true);
359         }
360 
361         public static string TitleCase(string input, bool ignoreShortWords)
362         {
363             List<string> ignoreWords = null;
364             if (ignoreShortWords)
365             {
366                 //TODO: Add more ignore words?
367                 ignoreWords = new List<string>();
368                 ignoreWords.Add("a");
369                 ignoreWords.Add("is");
370                 ignoreWords.Add("was");
371                 ignoreWords.Add("the");
372             }
373 
374             string[] tokens = input.Split(' ');
375             StringBuilder sb = new StringBuilder(input.Length);
376             foreach (string s in tokens)
377             {
378                 if (ignoreShortWords == true
379                     && s != tokens[0]
380                     && ignoreWords.Contains(s.ToLower()))
381                 {
382                     sb.Append(s + " ");
383                 }
384                 else
385                 {
386                     sb.Append(s[0].ToString().ToUpper());
387                     sb.Append(s.Substring(1).ToLower());
388                     sb.Append(" ");
389                 }
390             }
391 
392             return sb.ToString().Trim();
393         }
394 
395         /**//// <summary>
396         /// 去除字符串内的空白字符
397         /// </summary>
398         /// <param name="input">要进行处理的字符串</param>
399         /// <returns>处理后的字符串</returns>
400         public static string TrimIntraWords(string input)
401         {
402             Regex regEx = new Regex(@"[\s]+");
403             return regEx.Replace(input, " ");
404         }
405 
406         /**//// <summary>
407         /// 换行符转换成Html标签的换行符<br />
408         /// </summary>
409         /// <param name="input">要进行处理的字符串</param>
410         /// <returns>处理后的字符串</returns>
411         public static string NewLineToBreak(string input)
412         {
413             Regex regEx = new Regex(@"[\n|\r]+");
414             return regEx.Replace(input, "<br />");
415         }
416 
417         /**//// <summary>
418         /// 插入换行符(不中断单词)
419         /// </summary>
420         /// <param name="input">要进行处理的字符串</param>
421         /// <param name="charCount">每行字符数</param>
422         /// <returns>处理后的字符串</returns>
423         public static string WordWrap(string input, int charCount)
424         {
425             return StringHelper.WordWrap(input, charCount, false, Environment.NewLine);
426         }
427 
428         /**//// <summary>
429         /// 插入换行符
430         /// </summary>
431         /// <param name="input">要进行处理的字符串</param>
432         /// <param name="charCount">每行字符数</param>
433         /// <param name="cutOff">如果为真,将在单词的中部断开</param>
434         /// <returns>处理后的字符串</returns>
435         public static string WordWrap(string input, int charCount, bool cutOff)
436         {
437             return StringHelper.WordWrap(input, charCount, cutOff, Environment.NewLine);
438         }
439 
440         /**//// <summary>
441         /// 插入换行符
442         /// </summary>
443         /// <param name="input">要进行处理的字符串</param>
444         /// <param name="charCount">每行字符数</param>
445         /// <param name="cutOff">如果为真,将在单词的中部断开</param>
446         /// <param name="breakText">插入的换行符号</param>
447         /// <returns>处理后的字符串</returns>
448         public static string WordWrap(string input, int charCount, bool cutOff, string breakText)
449         {
450             StringBuilder sb = new StringBuilder(input.Length + 100);
451             int counter = 0;
452 
453             if (cutOff)
454             {
455                 while (counter < input.Length)
456                 {
457                     if (input.Length > counter + charCount)
458                     {
459                         sb.Append(input.Substring(counter, charCount));
460                         sb.Append(breakText);
461                     }
462                     else
463                     {
464                         sb.Append(input.Substring(counter));
465                     }
466                     counter += charCount;
467                 }
468             }
469             else
470             {
471                 string[] strings = input.Split(' ');
472                 for (int i = 0; i < strings.Length; i++)
473                 {
474                     counter += strings[i].Length + 1; // the added one is to represent the inclusion of the space.
475                     if (i != 0 && counter > charCount)
476                     {
477                         sb.Append(breakText);
478                         counter = 0;
479                     }
480 
481                     sb.Append(strings[i] + ' ');
482                 }
483             }
484             return sb.ToString().TrimEnd(); // to get rid of the extra space at the end.
485         }
486         #endregion
487     }
488 }

 

转载于:https://www.cnblogs.com/renjun/archive/2013/04/25/3041748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值