收藏---很實用到類 (防止sql注入,字符串操作等。。。。。。。。。)

1 using System;
   2  using System.Collections.Generic;
   3 using System.Text;
   4 using System.Collections;
   5 using System.Text.RegularExpressions;
   6 using System.Security.Cryptography;
   7 /**/
   8 ////////////////////////////////////////////////////
   9 ///功能:字符文本操作类
  10 ///
  11 ///
  12 ////////////////////////////////////////////////////
  13 namespace XHW
  14 {
  15     /// <summary>
  16     /// 字符文本操作类
  17     /// </summary>
  18     public class StringHelper
  19     {
  20         public static bool IsContains(string[] strs, string value)
  21         {
  22             if (strs == null)
  23             {
  24                 return false;
  25             }
  26
  27             foreach (string str in strs)
  28             {
  29                 if (str == value)
  30                 {
  31                     return true;
  32                 }
  33             }
  34
  35             return false;
  36         }
  37
  38
  39
  40         #region 字符串过滤
  41
  42         #region 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
  43         /**/
  44         /// <summary>
  45         /// 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
  46         /// </summary>
  47         /// <param name="source"></param>
  48         /// <returns></returns>
  49         #endregion
  50         public static string EncodeToHtml(string source)
  51         {
  52             source = source.Trim();
  53             source = source.Replace("'", "''");
  54             source = source.Replace("\\", "");
  55             source = System.Web.HttpContext.Current.Server.HtmlEncode(source);
  56             source = source.Replace("\r\n", "<br>");
  57             source = source.Replace("\n", "<br>");
  58             return source;
  59         }
  60
  61
  62         #region [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
  63         /**/
  64         /// <summary>
  65         /// [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 (不符合命名规范,请使用 EncodeToHtml 方法 )
  66         /// </summary>
  67         /// <param name="source"></param>
  68         /// <returns></returns>
  69         #endregion
  70         public static string HtmlFilterForInput(string source)
  71         {
  72             return EncodeToHtml(source);
  73         }
  74
  75
  76         #region 还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
  77         /**/
  78         /// <summary>
  79         /// 还原HTML编码为字符串,用于返回到input或 Textarea 输入框
  80         /// </summary>
  81         /// <param name="source"></param>
  82         /// <returns></returns>
  83         #endregion
  84         public static string DecodeFormHtml(string source)
  85         {
  86             source = source.Trim();
  87             source = source.Replace("<br>", "\r\n");
  88             source = source.Replace("<br>", "\n");
  89             source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
  90             return source;
  91         }
  92
  93
  94         #region [否决的]还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
  95         /**/
  96         /// <summary>
  97         /// [否决的]还原HTML编码为字符串,用于返回到input或 Textarea 输入框 (不符合命名规范,请使用 DecodeFormHtml 方法 )
  98         /// </summary>
  99         /// <param name="source"></param>
100         /// <returns></returns>
101         #endregion
102         public static string DeHtmlFilterForInput(string source)
103         {
104             source = source.Trim();
105             source = source.Replace("<br>", "\r\n");
106             source = source.Replace("<br>", "\n");
107             source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
108             return source;
109         }
110
111
112         #region 检验用户提交的URL参数字符里面是否有非法字符
113         /**/
114         /// <summary>
115         /// 检验用户提交的URL参数字符里面是否有非法字符,如果有则返回True.防止SQL注入.
116         /// </summary>
117         /// <param name="str">(string)</param>
118         /// <returns>bool</returns>
119         public static bool VerifyString(string str)
120         {
121             string strTmp = str.ToUpper();
122             if (strTmp.IndexOf("SELECT ") >= 0 || strTmp.IndexOf(" AND ") >= 0 || strTmp.IndexOf(" OR ") >= 0 ||
123                 strTmp.IndexOf("EXEC ") >= 0 || strTmp.IndexOf("CHAR(") >= 0)
124             {
125                 return true;
126             }
127
128             strTmp.Replace("'", "").Replace(";", "");
129             return false;
130         }
131
132         #endregion
133
134
135         #region 过滤 Sql 语句字符串中的注入脚本
136         /**/
137         /// <summary>
138         /// 过滤 Sql 语句字符串中的注入脚本
139         /// </summary>
140         /// <param name="source">传入的字符串</param>
141         /// <returns></returns>
142         #endregion
143         public static string FilterSql(string source)
144         {
145             //单引号替换成两个单引号
146             source = source.Replace("'", "''");
147             source = source.Replace("\"", "");
148             source = source.Replace("|", "");
149             //半角封号替换为全角封号,防止多语句执行
150             source = source.Replace(";", "");
151
152             //半角括号替换为全角括号
153             source = source.Replace("(", "");
154             source = source.Replace(")", "");
155
156             /**/
157             ///////////////要用正则表达式替换,防止字母大小写得情况////////////////////
158
159             //去除执行存储过程的命令关键字
160             source = source.Replace("Exec", "");
161             source = source.Replace("Execute", "");
162
163             //去除系统存储过程或扩展存储过程关键字
164             source = source.Replace("xp_", "x p_");
165             source = source.Replace("sp_", "s p_");
166
167             //防止16进制注入
168             source = source.Replace("0x", "0 x");
169
170             return source;
171         }
172
173
174         #region [否决的]过滤 Sql 语句字符串中的注入脚本
175         /**/
176         /// <summary>
177         /// [否决的]过滤 Sql 语句字符串中的注入脚本(请使用 FilterSql 方法 )
178         /// </summary>
179         /// <param name="source">传入的字符串</param>
180         /// <returns></returns>
181         #endregion
182         public static string SqlFilter(string source)
183         {
184             return FilterSql(source);
185         }
186
187
188         #region 过滤字符串只剩数字
189         /**/
190         /// <summary>
191         /// 过滤字符串只剩数字
192         /// </summary>
193         /// <param name="source">源字符串</param>
194         #endregion
195         public static string FilterToNumber(string source)
196         {
197             source = Regex.Replace(source, "[^0-9]*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
198             return source;
199         }
200
201
202         #region [否决的]过滤字符串只剩数字
203         /**/
204         /// <summary>
205         /// [否决的]过滤字符串只剩数字(请使用 FilterToNumber 方法)
206         /// </summary>
207         /// <param name="source">源字符串</param>
208         #endregion
209         public static string NumberFilter(string source)
210         {
211             source = Regex.Replace(source, "[^0-9]*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
212             return source;
213         }
214
215
216         #region 去除数组内重复元素
217         /**/
218         /// <summary>
219         /// 去除数组内重复元素
220         /// </summary>
221         /// <param name="arr"></param>
222         /// <returns></returns>
223         #endregion
224         public ArrayList FilterRepeatArrayItem(ArrayList arr)
225         {
226             //建立新数组
227             ArrayList newArr = new ArrayList();
228
229             //载入第一个原数组元素
230             if (arr.Count > 0)
231             {
232                 newArr.Add(arr[0]);
233             }
234
235             //循环比较
236             for (int i = 0; i < arr.Count; i++)
237             {
238                 if (!newArr.Contains(arr[i]))
239                 {
240                     newArr.Add(arr[i]);
241                 }
242             }
243             return newArr;
244         }
245
246
247         #region 在最后去除指定的字符
248         /**/
249         /// <summary>
250         /// 在最后去除指定的字符
251         /// </summary>
252         /// <param name="source">参加处理的字符</param>
253         /// <param name="cutString">要去除的字符</param>
254         /// <returns>返回结果</returns>
255         #endregion
256         public static string CutLastString(string source, string cutString)
257         {
258             string result = "";
259             int tempIndex = 0;
260
261             tempIndex = source.LastIndexOf(cutString);
262             if (cutString.Length == (source.Length - tempIndex))
263             {
264                 result = source.Substring(0, tempIndex);
265             }
266             else
267             {
268                 result = source;
269             }
270
271             return result;
272         }
273
274
275         #region 利用正则表达式实现UBB代码转换为html代码
276         /**/
277         /// <summary>
278         /// 利用正则表达式实现UBB代码转换为html代码
279         /// </summary>
280         /// <param name="source">待处理的文本内容</param>
281         /// <returns>返回正确的html代码</returns>
282         #endregion
283         public static string UBBCode(string source)
284         {
285             if (source == null || source.Length == 0)
286             {
287                 return "";
288             }
289
290             source = source.Replace("&nbsp;", " ");
291             //source=source.Replace("<","&lt");
292             //source=source.Replace(">","&gt");
293             source = source.Replace("\n", "<br>");
294             source = Regex.Replace(source, @"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]", @"<a href=$1 target=_blank>$2</a>", RegexOptions.IgnoreCase);
295             source = Regex.Replace(source, @"\[url\](?<x>[^\]]*)\[/url\]", @"<a href=$1 target=_blank>$1</a>", RegexOptions.IgnoreCase);
296             source = Regex.Replace(source, @"\[email=(?<x>[^\]]*)\](?<y>[^\]]*)\[/email\]", @"<a href=$1>$2</a>", RegexOptions.IgnoreCase);
297             source = Regex.Replace(source, @"\[email\](?<x>[^\]]*)\[/email\]", @"<a href=$1>$1</a>", RegexOptions.IgnoreCase);
298             source = Regex.Replace(source, @"\[flash](?<x>[^\]]*)\[/flash]", @"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>", RegexOptions.IgnoreCase);
299             source = Regex.Replace(source, @"\", @"<IMG src=""$1"" border=0>", RegexOptions.IgnoreCase);
300             source = Regex.Replace(source, @"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]", @"<font color=$1>$2</font>", RegexOptions.IgnoreCase);
301             source = Regex.Replace(source, @"\[face=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]", @"<font face=$1>$2</font>", RegexOptions.IgnoreCase);
302             source = Regex.Replace(source, @"\[size=1\](?<x>[^\]]*)\[/size\]", @"<font size=1>$1</font>", RegexOptions.IgnoreCase);
303             source = Regex.Replace(source, @"\[size=2\](?<x>[^\]]*)\[/size\]", @"<font size=2>$1</font>", RegexOptions.IgnoreCase);
304             source = Regex.Replace(source, @"\[size=3\](?<x>[^\]]*)\[/size\]", @"<font size=3>$1</font>", RegexOptions.IgnoreCase);
305             source = Regex.Replace(source, @"\[size=4\](?<x>[^\]]*)\[/size\]", @"<font size=4>$1</font>", RegexOptions.IgnoreCase);
306             source = Regex.Replace(source, @"\[size=5\](?<x>[^\]]*)\[/size\]", @"<font size=5>$1</font>", RegexOptions.IgnoreCase);
307             source = Regex.Replace(source, @"\[size=6\](?<x>[^\]]*)\[/size\]", @"<font size=6>$1</font>", RegexOptions.IgnoreCase);
308             source = Regex.Replace(source, @"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]", @"<align=$1>$2</align>", RegexOptions.IgnoreCase);
309             source = Regex.Replace(source, @"\[fly](?<x>[^\]]*)\[/fly]", @"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>", RegexOptions.IgnoreCase);
310             source = Regex.Replace(source, @"\[move](?<x>[^\]]*)\[/move]", @"<marquee scrollamount=3>$1</marquee>", RegexOptions.IgnoreCase);
311             source = Regex.Replace(source, @"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]", @"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>", RegexOptions.IgnoreCase);
312             source = Regex.Replace(source, @"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]", @"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>", RegexOptions.IgnoreCase);
313             source = Regex.Replace(source, @"\[center\](?<x>[^\]]*)\[/center\]", @"<center>$1</center>", RegexOptions.IgnoreCase);
314             source = Regex.Replace(source, @"\[b\](?<x>[^\]]*)\[/b\]", @"<b>$1</b>", RegexOptions.IgnoreCase);
315             source = Regex.Replace(source, @"\[i\](?<x>[^\]]*)\[/i\]", @"<i>$1</i>", RegexOptions.IgnoreCase);
316             source = Regex.Replace(source, @"\[u\](?<x>[^\]]*)\[/u\]", @"<u>$1</u>", RegexOptions.IgnoreCase);
317             source = Regex.Replace(source, @"\[code\](?<x>[^\]]*)\[/code\]", @"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>", RegexOptions.IgnoreCase);
318             source = Regex.Replace(source, @"\[list\](?<x>[^\]]*)\[/list\]", @"<ul>$1</ul>", RegexOptions.IgnoreCase);
319             source = Regex.Replace(source, @"\[list=1\](?<x>[^\]]*)\[/list\]", @"<ol type=1>$1</ol id=1>", RegexOptions.IgnoreCase);
320             source = Regex.Replace(source, @"\[list=a\](?<x>[^\]]*)\[/list\]", @"<ol type=a>$1</ol id=a>", RegexOptions.IgnoreCase);
321             source = Regex.Replace(source, @"\[\*\](?<x>[^\]]*)\[/\*\]", @"<li>$1</li>", RegexOptions.IgnoreCase);
322             source = Regex.Replace(source, @"\[quote](?<x>.*)\[/quote]", @"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0' ><tr><td>$1</td></tr></table></center>", RegexOptions.IgnoreCase);
323             source = Regex.Replace(source, @"\[QT=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/QT]", @"<embed src=$3 width=$1 height=$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>", RegexOptions.IgnoreCase);
324             source = Regex.Replace(source, @"\[MP=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/MP]", @"<object align=center classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$3 width=$1 height=$2></embed></object>", RegexOptions.IgnoreCase);
325             source = Regex.Replace(source, @"\[RM=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/RM]", @"<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>", RegexOptions.IgnoreCase);
326             return (source);
327         }
328
329
330         #region 整理(过滤)以英文逗号分割的字符串
331         /**/
332         /// <summary>
333         /// 整理(过滤)以英文逗号分割的字符串
334         /// </summary>
335         /// <param name="source">原字符串</param>
336         /// <param name="str2">待清除的字符串,如空格</param>
337         /// <returns></returns>
338         #endregion
339         public static string FilterStringArray(string source, string str2)
340         {
341             source = source.Replace(str2, "");
342             if (source != "")
343             {
344                 source = source.Replace(",,", ",");
345
346                 if (source[0].ToString() == ",")
347                 {
348                     source = source.Substring(1, source.Length - 1);
349                 }
350
351                 if (source[source.Length - 1].ToString() == ",")
352                 {
353                     source = source.Substring(0, source.Length - 1);
354                 }
355             }
356             return source;
357         }
358
359
360         #endregion
361
362         #region 字符串组合
363
364         #region 返回年月日时分秒组合的字符串
365         /**/
366         /// <summary>
367         /// 返回年月日时分秒组合的字符串,如:20050424143012
368         /// </summary>
369         /// <param name="splitString">中间间隔的字符串,如2005\04\24\14\30\12。可以用来建立目录时使用</param>
370         /// <returns></returns>
371         #endregion
372         public static string GetTimeString()
373         {
374             //DateTime now = DateTime.Now;
375
376             //StringBuilder sb = new StringBuilder();
377             //sb.Append(now.Year.ToString("0000"));
378             //sb.Append(splitString);
379             //sb.Append(now.Month.ToString("00"));
380             //sb.Append(splitString);
381             //sb.Append(now.Day.ToString("00"));
382             //sb.Append(splitString);
383             //sb.Append(now.Hour.ToString("00"));
384             //sb.Append(splitString);
385             //sb.Append(now.Minute.ToString("00"));
386             //sb.Append(splitString);
387             //sb.Append(now.Second.ToString("00"));
388           string kk=Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");
389
390           return kk;
391         }
392
393
394         #region 返回年月日时分秒组合的字符串
395         /**/
396         /// <summary>
397         /// 返回年月日组合的字符串,如:20050424 (2005年4月24日)
398         /// </summary>
399         /// <param name="splitString">中间间隔的字符串,如2005\04\24 可以用来建立目录时使用</param>
400         /// <returns></returns>
401         #endregion
402         public static string GetDateString()
403         {
404             //DateTime now = DateTime.Now;
405
406             //StringBuilder sb = new StringBuilder();
407             //sb.Append(now.Year.ToString("0000"));
408             //sb.Append(splitString);
409             //sb.Append(now.Month.ToString("00"));
410             //sb.Append(splitString);
411             //sb.Append(now.Day.ToString("00"));
412             string kk = Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");
413             return kk;
414         }
415
416
417         #endregion
418
419         #region 随机字符串,随机数
420
421         private static string _LowerChar = "abcdefghijklmnopqrstuvwxyz";
422         private static string _UpperChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
423         private static string _NumberChar = "0123456789";
424
425         #region 获取种子
426         /**/
427         /// <summary>
428         /// 使用RNGCryptoServiceProvider 做种,可以在一秒内产生的随机数重复率非常
429         /// 的低,对于以往使用时间做种的方法是个升级
430         /// </summary>
431         /// <returns></returns>
432         #endregion
433         public static int GetNewSeed()
434         {
435             byte[] rndBytes = new byte[4];
436             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
437             rng.GetBytes(rndBytes);
438             return BitConverter.ToInt32(rndBytes, 0);
439         }
440
441
442         #region 取得指定范围内的数字随几数
443         /**/
444         /// <summary>
445         /// 取得指定范围内的随几数
446         /// </summary>
447         /// <param name="startNumber">下限数</param>
448         /// <param name="endNumber">上限数</param>
449         /// <returns>int</returns>
450         #endregion
451         public static int GetRandomNumber(int startNumber, int endNumber)
452         {
453             Random objRandom = new Random(GetNewSeed());
454             int r = objRandom.Next(startNumber, endNumber);
455             return r;
456         }
457
458
459         #region 获取指定 ASCII 范围内的随机字符串
460         /**/
461         /// <summary>
462         /// 获取指定 ASCII 范围内的随机字符串
463         /// </summary>
464         /// <param name="resultLength">结果字符串长度</param>
465         /// <param name="startNumber"> 开始的ASCII值 如(33-125)中的 33</param>
466         /// <param name="endNumber"> 结束的ASCII值 如(33-125)中的 125</param>
467         /// <returns></returns>
468         #endregion
469         public static string GetRandomStringByASCII(int resultLength, int startNumber, int endNumber)
470         {
471             System.Random objRandom = new System.Random(GetNewSeed());
472             string result = null;
473             for (int i = 0; i < resultLength; i++)
474             {
475                 result += (char)objRandom.Next(startNumber, endNumber);
476             }
477             return result;
478         }
479
480
481         #region 从指定字符串中抽取指定长度的随机字符串
482         /**/
483         /// <summary>
484         /// 从指定字符串中抽取指定长度的随机字符串
485         /// </summary>
486         /// <param name="source">源字符串</param>
487         /// <param name="resultLength">待获取随机字符串长度</param>
488         /// <returns></returns>
489         #endregion
490         private static string GetRandomString(string source, int resultLength)
491         {
492             System.Random objRandom = new System.Random(GetNewSeed());
493             string result = null;
494             for (int i = 0; i < resultLength; i++)
495             {
496                 result += source.Substring(objRandom.Next(0, source.Length - 1), 1);
497             }
498             return result;
499         }
500
501
502         #region 获取指定长度随机的数字字符串
503         /**/
504         /// <summary>
505         /// 获取指定长度随机的数字字符串
506         /// </summary>
507         /// <param name="resultLength">待获取随机字符串长度</param>
508         /// <returns></returns>
509         #endregion
510         public static string GetRandomNumberString(int resultLength)
511         {
512             return GetRandomString(_NumberChar, resultLength);
513         }
514
515
516         #region 获取指定长度随机的字母字符串(包含大小写字母)
517         /**/
518         /// <summary>
519         /// 获取指定长度随机的字母字符串(包含大小写字母)
520         /// </summary>
521         /// <param name="resultLength">待获取随机字符串长度</param>
522         /// <returns></returns>
523         #endregion
524         public static string GetRandomLetterString(int resultLength)
525         {
526             return GetRandomString(_LowerChar + _UpperChar, resultLength);
527         }
528
529
530         #region 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
531         /**/
532         /// <summary>
533         /// 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
534         /// </summary>
535         /// <param name="resultLength">待获取随机字符串长度</param>
536         /// <returns></returns>
537         #endregion
538         public static string GetRandomMixString(int resultLength)
539         {
540             return GetRandomString(_LowerChar + _UpperChar + _NumberChar, resultLength);
541         }
542
543         #endregion
544
545         #region 字符串验证
546
547         #region 判断字符串是否为整型
548         /**/
549         /// <summary>
550         /// 判断字符串是否为整型
551         /// </summary>
552         /// <param name="source"></param>
553         /// <returns></returns>
554         #endregion
555         public static bool IsInteger(string source)
556         {
557             if (source == null || source == "")
558             {
559                 return false;
560             }
561
562             if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
563             {
564                 return true;
565             }
566             else
567             {
568                 return false;
569             }
570         }
571
572
573         #region Email 格式是否合法
574         /**/
575         /// <summary>
576         /// Email 格式是否合法
577         /// </summary>
578         /// <param name="strEmail"></param>
579         #endregion
580         public static bool IsEmail(string strEmail)
581         {
582             return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
583         }
584
585
586         #region 判断是否IP
587         /**/
588         /// <summary>
589         /// 判断是否IP
590         /// </summary>
591         /// <param name="source"></param>
592         /// <returns></returns>
593         #endregion
594         public static bool IsIP(string source)
595         {
596             return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
597         }
598
599
600         #region 检查字符串是否为A-Z、0-9及下划线以内的字符
601
602         /**/
603         /// <summary>
604         /// 检查字符串是否为A-Z、0-9及下划线以内的字符
605         /// </summary>
606         /// <param name="str">被检查的字符串</param>
607         /// <returns>是否有特殊字符</returns>
608         #endregion
609         public static bool IsLetterOrNumber(string str)
610         {
611             bool b = System.Text.RegularExpressions.Regex.IsMatch(str, "[a-zA-Z0-9_]");
612             return b;
613         }
614
615
616         #region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
617         /**/
618         /// <summary>
619         /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
620         /// </summary>
621         /// <param name="source"></param>
622         /// <returns></returns>
623         #endregion
624         public static bool IsSpecialChar(string source)
625         {
626             Regex r = new Regex(@"[/\<>:.?*|$]");
627             return r.IsMatch(source);
628         }
629
630
631         #region 是否全为中文/日文/韩文字符
632         /**/
633         /// <summary>
634         /// 是否全为中文/日文/韩文字符
635         /// </summary>
636         /// <param name="source">源字符串</param>
637         /// <returns></returns>
638         #endregion
639         public static bool IsChineseChar(string source)
640         {
641             //中文/日文/韩文: [\u4E00-\u9FA5]
642             //英文:[a-zA-Z]
643             return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
644         }
645
646
647         #region 是否包含双字节字符(允许有单字节字符)
648         /**/
649         /// <summary>
650         /// 是否包含双字节字符(允许有单字节字符)
651         /// </summary>
652         /// <param name="source"></param>
653         /// <returns></returns>
654         #endregion
655         public static bool IsDoubleChar(string source)
656         {
657             return Regex.IsMatch(source, @"[^\x00-\xff]");
658         }
659
660
661         #region 是否为日期型字符串
662         /**/
663         /// <summary>
664         /// 是否为日期型字符串
665         /// </summary>
666         /// <param name="source">日期字符串(2005-6-30)</param>
667         /// <returns></returns>
668         #endregion
669         public static bool IsDate(string source)
670         {
671             return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
672         }
673
674
675         #region 是否为时间型字符串
676         /**/
677         /// <summary>
678         /// 是否为时间型字符串
679         /// </summary>
680         /// <param name="source">时间字符串(15:00:00)</param>
681         /// <returns></returns>
682         #endregion
683         public static bool IsTime(string source)
684         {
685             return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
686         }
687
688
689         #region 是否为日期+时间型字符串
690         /**/
691         /// <summary>
692         /// 是否为日期+时间型字符串
693         /// </summary>
694         /// <param name="source"></param>
695         /// <returns></returns>
696         #endregion
697         public static bool IsDateTime(string source)
698         {
699             return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
700         }
701
702
703         #region 是否为钱币型数据
704         /**/
705         /// <summary>
706         /// 是否为钱币型数据
707         /// </summary>
708         /// <param name="source"></param>
709         /// <returns></returns>
710         #endregion
711         public static bool IsMoney(string source)
712         {
713             return false;
714         }
715
716         #endregion
717
718         #region 字符串截取
719
720         #region 获取字符串的实际长度(按单字节)
721         /**/
722         /// <summary>
723         /// 获取字符串的实际长度(按单字节)
724         /// </summary>
725         /// <param name="source"></param>
726         /// <returns></returns>
727         #endregion
728         public static int GetRealLength(string source)
729         {
730             return System.Text.Encoding.Default.GetByteCount(source);
731         }
732
733
734         #region 取得固定长度的字符串(按单字节截取)
735         /**/
736         /// <summary>
737         /// 取得固定长度的字符串(按单字节截取)。
738         /// </summary>
739         /// <param name="source">源字符串</param>
740         /// <param name="resultLength">截取长度</param>
741         /// <returns></returns>
742         #endregion
743         public static string SubString(string source, int resultLength)
744         {
745
746             //判断字符串长度是否大于截断长度
747             if (System.Text.Encoding.Default.GetByteCount(source) > resultLength)
748             {
749                 //判断字串是否为空
750                 if (source == null)
751                 {
752                     return "";
753                 }
754
755                 //初始化
756                 int i = 0, j = 0;
757
758                 //为汉字或全脚符号长度加2否则加1
759                 foreach (char newChar in source)
760                 {
761                     if ((int)newChar > 127)
762                     {
763                         i += 2;
764                     }
765                     else
766                     {
767                         i++;
768                     }
769                     if (i > resultLength)
770                     {
771                         source = source.Substring(0, j);
772                         break;
773                     }
774                     j++;
775                 }
776             }
777             return source;
778         }
779
780
781         #region 按长度分割字符串
782         /**/
783         /// <summary>
784         /// 按长度分割字符串,如短信
785         /// </summary>
786         /// <param name="str"></param>
787         /// <param name="len"></param>
788         /// <returns></returns>
789         #endregion
790         private ArrayList SplitStringByLength(string str, int len)
791         {
792             ArrayList arrBlock = new ArrayList();
793             int intBlockCount = str.Length / len;
794             if (str.Length % len != 0)
795             {
796                 for (int i = 0; i <= intBlockCount; i++)
797                 {
798                     if ((str.Length - i * len) > len)
799                         arrBlock.Add(str.Substring(i * len, len));
800                     else
801                         arrBlock.Add(str.Substring(i * len, (str.Length % len)));
802                 }
803             }
804             else
805             {
806                 for (int i = 0; i < intBlockCount; i++)
807                 {
808                     arrBlock.Add(str.Substring(i * len, len));
809                 }
810             }
811             return arrBlock;
812         }
813
814
815         #endregion
816
817         #region 字符串比较
818
819         /**/
820         /// <summary>
821         ///  获得某个字符串在另个字符串中出现的次数
822         /// </summary>
823         /// <param name="strOriginal">要处理的字符</param>
824         /// <param name="strSymbol">符号</param>
825         /// <returns>返回值</returns>
826         public static int GetStringIncludeCount(string strOriginal, string strSymbol)
827         {
828
829             int count = 0;
830             count = strOriginal.Length - strOriginal.Replace(strSymbol, String.Empty).Length;
831             return count;
832         }
833
834         /**/
835         /// <summary>
836         /// 获得某个字符串在另个字符串第一次出现时前面所有字符
837         /// </summary>
838         /// <param name="strOriginal">要处理的字符</param>
839         /// <param name="strSymbol">符号</param>
840         /// <returns>返回值</returns>
841         public static string GetFirstString(string strOriginal, string strSymbol)
842         {
843             int strPlace = strOriginal.IndexOf(strSymbol);
844             if (strPlace != -1)
845             {
846                 strOriginal = strOriginal.Substring(0, strPlace);
847             }
848             return strOriginal;
849         }
850
851         /**/
852         /// <summary>
853         /// 获得某个字符串在另个字符串最后一次出现时后面所有字符
854         /// </summary>
855         /// <param name="strOriginal">要处理的字符</param>
856         /// <param name="strSymbol">符号</param>
857         /// <returns>返回值</returns>
858         public static string GetLastString(string strOriginal, string strSymbol)
859         {
860             int strPlace = strOriginal.LastIndexOf(strSymbol) + strSymbol.Length;
861             strOriginal = strOriginal.Substring(strPlace);
862             return strOriginal;
863         }
864
865         /**/
866         /// <summary>
867         /// 获得两个字符之间第一次出现时前面所有字符
868         /// </summary>
869         /// <param name="strOriginal">要处理的字符</param>
870         /// <param name="strFirst">最前哪个字符</param>
871         /// <param name="strLast">最后哪个字符</param>
872         /// <returns>返回值</returns>
873         public static string GetTwoMiddleFirstStr(string strOriginal, string strFirst, string strLast)
874         {
875             strOriginal = GetFirstString(strOriginal, strLast);
876             strOriginal = GetLastString(strOriginal, strFirst);
877             return strOriginal;
878         }
879
880         /**/
881         /// <summary>
882         ///  获得两个字符之间最后一次出现时的所有字符
883         /// </summary>
884         /// <param name="strOriginal">要处理的字符</param>
885         /// <param name="strFirst">最前哪个字符</param>
886         /// <param name="strLast">最后哪个字符</param>
887         /// <returns>返回值</returns>
888         public static string GetTwoMiddleLastStr(string strOriginal, string strFirst, string strLast)
889         {
890             strOriginal = GetLastString(strOriginal, strFirst);
891             strOriginal = GetFirstString(strOriginal, strLast);
892             return strOriginal;
893         }
894
895         /**/
896         /// <summary>
897         /// 发帖过滤词(用“|”号分隔)Application["app_state_FilterWord"]
898         /// </summary>
899         /// <param name="str">字符串</param>
900         /// <param name="chkword">过滤词(用“|”号分隔)</param>
901         public static bool CheckBadWords(string str, string chkword)
902         {
903             if (chkword != null && chkword != "")
904             {
905                 string filter = chkword;
906                 string chk1 = "";
907                 string[] aryfilter = filter.Split('|');
908                 for (int i = 0; i < aryfilter.Length; i++)
909                 {
910                     chk1 = aryfilter[i].ToString();
911                     if (str.IndexOf(chk1) >= 0)
912                         return true;
913                 }
914             }
915             return false;
916         }
917
918         /**/
919         /// <summary>
920         /// 发帖过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)Application["app_state_Check_FilterWord"]
921         /// </summary>
922         /// <param name="str">字符串</param>
923         /// <param name="chkword">过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)</param>
924         public static bool CheckilterStr(string str, string chkword)
925         {
926
927             if (chkword != null && chkword != "")
928             {
929                 string filter = chkword;
930                 string[] aryfilter = filter.Split('§');
931                 string[] aryfilter_lei;
932                 int lei_for = 0, j;
933
934                 for (int i = 0; i < aryfilter.Length; i++)
935                 {
936                     lei_for = 0;
937                     aryfilter_lei = aryfilter[i].Split(',');
938                     for (j = 0; j < aryfilter_lei.Length; j++)
939                     {
940                         if (str.IndexOf(aryfilter_lei[j].ToString()) >= 0)
941                             lei_for += 1;
942                     }
943                     if (lei_for == aryfilter_lei.Length)
944                         return true;
945                 }
946             }
947             return false;
948         }
949
950
951         #endregion
952
953         #region 字符集转换
954
955         #region 将 GB2312 值转换为 UTF8 字符串(如:测试 -> 娴嬭瘯 )
956         /**/
957         /// <summary>
958         /// 将 GB2312 值转换为 UTF8 字符串(如:测试 -> 娴嬭瘯 )
959         /// </summary>
960         /// <param name="source"></param>
961         /// <returns></returns>
962         #endregion
963         public static string ConvertGBToUTF8(string source)
964         {
965             /**/
966             /*
967         byte[] bytes;
968         bytes = System.Text.Encoding.Default.GetBytes(source);
969         System.Text.Decoder gbDecoder = System.Text.Encoding.GetEncoding("gb2312").GetDecoder();
970         int charCount = gbDecoder.GetCharCount(bytes,0 , bytes.Length);
971         Char[] chars = new Char[charCount];
972         int charsDecodedCount = gbDecoder.GetChars(bytes,0,bytes.Length,chars,0);
973         return new string(chars);
974         */
975
976             return Encoding.GetEncoding("GB2312").GetString(Encoding.UTF8.GetBytes(source));
977         }
978
979
980         #region 将 UTF8 值转换为 GB2312 字符串 (如:娴嬭瘯 -> 测试)
981         /**/
982         /// <summary>
983         /// 将 UTF8 值转换为 GB2312 字符串 (如:娴嬭瘯 -> 测试)
984         /// </summary>
985         /// <param name="source"></param>
986         /// <returns></returns>
987         #endregion
988         public static string ConvertUTF8ToGB(string source)
989         {
990             /**/
991             /*
992         byte[] bytes;
993         //模拟编码
994         System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
995         bytes = utf8.GetBytes(source);
996            
997         //开始解码
998         System.Text.Decoder utf8Decoder = System.Text.Encoding.UTF8.GetDecoder();
999
1000         int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.Length);
1001         Char[] chars = new Char[charCount];
1002         int charsDecodedCount = utf8Decoder.GetChars(bytes, 0, bytes.Length, chars, 0);
1003
1004         return new string(chars);
1005         */
1006
1007             return Encoding.UTF8.GetString(Encoding.GetEncoding("GB2312").GetBytes(source));
1008         }
1009
1010
1011         #region 由16进制转为汉字字符串(如:B2E2 -> 测 )
1012         /**/
1013         /// <summary>
1014         /// 由16进制转为汉字字符串(如:B2E2 -> 测 )
1015         /// </summary>
1016         /// <param name="source"></param>
1017         /// <returns></returns>
1018         #endregion
1019         public static string ConvertHexToString(string source)
1020         {
1021             byte[] oribyte = new byte[source.Length / 2];
1022             for (int i = 0; i < source.Length; i += 2)
1023             {
1024                 string str = Convert.ToInt32(source.Substring(i, 2), 16).ToString();
1025                 oribyte[i / 2] = Convert.ToByte(source.Substring(i, 2), 16);
1026             }
1027             return System.Text.Encoding.Default.GetString(oribyte);
1028         }
1029
1030
1031         #region  字符串转为16进制字符串(如:测 -> B2E2 )
1032         /**/
1033         /// <summary>
1034         /// 字符串转为16进制字符串(如:测 -> B2E2 )
1035         /// </summary>
1036         /// <param name="Word"></param>
1037         /// <returns></returns>
1038         #endregion
1039         public static string ConvertToHex(string Word)
1040         {
1041             int i = Word.Length;
1042             string temp;
1043             string end = "";
1044             byte[] array = new byte[2];
1045             int i1, i2;
1046             for (int j = 0; j < i; j++)
1047             {
1048                 temp = Word.Substring(j, 1);
1049                 array = System.Text.Encoding.Default.GetBytes(temp);
1050                 if (array.Length.ToString() == "1")
1051                 {
1052                     i1 = Convert.ToInt32(array[0]);
1053                     end += Convert.ToString(i1, 16);
1054                 }
1055                 else
1056                 {
1057                     i1 = Convert.ToInt32(array[0]);
1058                     i2 = Convert.ToInt32(array[1]);
1059                     end += Convert.ToString(i1, 16);
1060                     end += Convert.ToString(i2, 16);
1061                 }
1062             }
1063             return end.ToUpper();
1064         }
1065
1066
1067         #region 字符串转为unicode字符串(如:测试 -> 测试)
1068         /**/
1069         /// <summary>
1070         /// 字符串转为unicode字符串(如:测试 -> 测试)
1071         /// </summary>
1072         /// <param name="source"></param>
1073         /// <returns></returns>
1074         #endregion
1075         public static string ConvertToUnicode(string source)
1076         {
1077             StringBuilder sa = new StringBuilder();//Unicode
1078             string s1;
1079             string s2;
1080             for (int i = 0; i < source.Length; i++)
1081             {
1082                 byte[] bt = System.Text.Encoding.Unicode.GetBytes(source.Substring(i, 1));
1083                 if (bt.Length > 1)//判断是否汉字
1084                 {
1085                     s1 = Convert.ToString((short)(bt[1] - '\0'), 16);//转化为16进制字符串
1086                     s2 = Convert.ToString((short)(bt[0] - '\0'), 16);//转化为16进制字符串
1087                     s1 = (s1.Length == 1 ? "0" : "") + s1;//不足位补0
1088                     s2 = (s2.Length == 1 ? "0" : "") + s2;//不足位补0
1089                     sa.Append("&#" + Convert.ToInt32(s1 + s2, 16) + ";");
1090                 }
1091             }
1092
1093             return sa.ToString();
1094         }
1095
1096
1097         #region 字符串转为UTF8字符串(如:测试 -> \u6d4b\u8bd5)
1098         /**/
1099         /// <summary>
1100         /// 字符串转为UTF8字符串(如:测试 -> \u6d4b\u8bd5)
1101         /// </summary>
1102         /// <param name="source"></param>
1103         /// <returns></returns>
1104         #endregion
1105         public static string ConvertToUTF8(string source)
1106         {
1107             StringBuilder sb = new StringBuilder();//UTF8
1108             string s1;
1109             string s2;
1110             for (int i = 0; i < source.Length; i++)
1111             {
1112                 byte[] bt = System.Text.Encoding.Unicode.GetBytes(source.Substring(i, 1));
1113                 if (bt.Length > 1)//判断是否汉字
1114                 {
1115                     s1 = Convert.ToString((short)(bt[1] - '\0'), 16);//转化为16进制字符串
1116                     s2 = Convert.ToString((short)(bt[0] - '\0'), 16);//转化为16进制字符串
1117                     s1 = (s1.Length == 1 ? "0" : "") + s1;//不足位补0
1118                     s2 = (s2.Length == 1 ? "0" : "") + s2;//不足位补0
1119                     sb.Append("\\u" + s1 + s2);
1120                 }
1121             }
1122
1123             return sb.ToString();
1124         }
1125
1126
1127         #region 转化为ASC码方法
1128         //转化为ASC码方法
1129
1130         public string ConvertToAsc(string txt)
1131         {
1132             string newtxt = "";
1133             foreach (char c in txt)
1134             {
1135
1136                 newtxt += Convert.ToString((int)c);
1137             }
1138             return newtxt;
1139
1140         }
1141         #endregion
1142
1143         #region BASE64
1144
1145         /**/
1146         /// <summary>
1147         /// 将字符串使用base64算法加密
1148         /// </summary>
1149         /// <param name="source">待加密的字符串</param>
1150         /// <returns>加码后的文本字符串</returns>
1151         public static string Base64_Encrypt(string source)
1152         {
1153             return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(source));
1154         }
1155
1156         /**/
1157         /// <summary>
1158         /// 从Base64编码的字符串中还原字符串,支持中文
1159         /// </summary>
1160         /// <param name="source">Base64加密后的字符串</param>
1161         /// <returns>还原后的文本字符串</returns>
1162         public static string Base64_Decrypt(string source)
1163         {
1164             return System.Text.Encoding.Default.GetString(Convert.FromBase64String(source));
1165         }
1166         #endregion
1167
1168         #endregion
1169
1170         #region 字符串格式化
1171
1172         #region 将字符串反转
1173         /**/
1174         /// <summary>
1175         /// 将字符串反转
1176         /// </summary>
1177         /// <param name="source"></param>
1178         /// <returns></returns>
1179         #endregion
1180         public static string FormatReverse(string source)
1181         {
1182             char[] ca = source.ToCharArray();
1183             Array.Reverse(ca);
1184             source = new String(ca);
1185             return source;
1186         }
1187
1188
1189         /**/
1190         /// <summary>
1191         /// 格式化为小数点两位的字符串(四舍五入)
1192         /// </summary>
1193         /// <param name="source"></param>
1194         /// <returns></returns>
1195         public static string FormatFloat2(double source)
1196         {
1197             //double tmp=1223333.24215265652;
1198             return source.ToString("##,###.000");
1199         }
1200
1201
1202         /**/
1203         /// <summary>
1204         /// 将数字字符串转成货币格式
1205         /// </summary>
1206         /// <returns></returns>
1207         public static string GetMoneyFormat()
1208         {
1209             /**/
1210             /*
1211         string.Format("${0:###,###,###.##}元",decimalVar);
1212         --------------------------------------------------------------------------------
1213         找到system.string的format方法
1214         string str="99988"
1215         str=format(str,"##,####.00")
1216         str value is:99,988.00
1217         试一下行不行
1218         --------------------------------------------------------------------------------
1219         string strMoney="544.54";
1220         decimal decMoney=System.Convert.ToDecimal(strMoney);
1221         string money=decMoney.ToString("c");
1222
1223         如果要转换成不同地区的符号,可以用:
1224         CultureInfo MyCulture =  new CultureInfo("zh-CN");
1225         String MyString = MyInt.ToString("C", MyCulture);
1226
1227         注意:这里OrderTotal的格式只能是Decimal.
1228
1229          */
1230             return "";
1231         }
1232
1233
1234         /**/
1235         /// <summary>
1236         /// 格式化字符串 字符串1234567,想变成1,234,567
1237         /// </summary>
1238         /// <param name="source"></param>
1239         /// <returns></returns>
1240         public static string GetO1(string source)
1241         {
1242             //return source.ToString("N");
1243             //return    source.ToString("###,###");
1244             return "";
1245         }
1246
1247
1248         /**/
1249         /// <summary>
1250         /// 十进制数字1转换为字符串0a,在前面补零
1251         /// </summary>
1252         /// <param name="source"></param>
1253         /// <returns></returns>
1254         public static string OctToHex(int source)
1255         {
1256             return source.ToString("X2");
1257         }
1258
1259
1260         /**/
1261         /// <summary>
1262         /// 将一个十六进制表示的字符串转成int型?如"0A" = 10
1263         /// </summary>
1264         /// <param name="source"></param>
1265         /// <returns></returns>
1266         public static string HexToOct(string source)
1267         {
1268             return Convert.ToInt16(source, 16).ToString();
1269         }
1270
1271
1272         #endregion
1273
1274         #region 身份证验证
1275
1276         #region 从18位身份证获取用户所在省份、生日、性别信息
1277         /**/
1278         /// <summary>
1279         /// 从18位身份证获取用户所在省份、生日、性别信息
1280         /// </summary>
1281         /// <param name="cid">身份证字符串</param>
1282         /// <returns>如:福建,1978-06-30,男</returns>
1283         #endregion
1284         public static string GetCidInfo(string cid)
1285         {
1286             string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "***", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
1287             double iSum = 0;
1288             System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
1289             System.Text.RegularExpressions.Match mc = rg.Match(cid);
1290             if (!mc.Success)
1291             {
1292                 return "";
1293             }
1294             cid = cid.ToLower();
1295             cid = cid.Replace("x", "a");
1296             if (aCity[int.Parse(cid.Substring(0, 2))] == null)
1297             {
1298                 return "非法地区";
1299             }
1300             try
1301             {
1302                 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
1303             }
1304             catch
1305             {
1306                 return "非法生日";
1307             }
1308             for (int i = 17; i >= 0; i--)
1309             {
1310                 iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
1311
1312             }
1313             if (iSum % 11 != 1)
1314                 return ("非法证号");
1315
1316             return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "" : ""));
1317
1318         }
1319
1320
1321         #region 十五位的身份证号转为十八位的
1322         /**/
1323         /// <summary>
1324         /// 十五位的身份证号转为十八位的
1325         /// </summary>
1326         /// <param name="source">十五位的身份证号</param>
1327         /// <returns></returns>
1328         #endregion
1329         public static string Cid15To18(string source)
1330         {
1331             string[] arrInt = new string[17] { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
1332             string[] arrCh = new string[11] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
1333
1334             int nTemp = 0, i;
1335
1336             if (source.Length == 15)
1337             {
1338                 source = source.Substring(0, 6) + "19" + source.Substring(6, source.Length - 6);
1339                 for (i = 0; i < source.Length; i++)
1340                 {
1341                     nTemp += int.Parse(source.Substring(i, 1)) * int.Parse(arrInt[i]);
1342                 }
1343                 source += arrCh[nTemp % 11];
1344
1345                 return source;
1346             }
1347             else
1348             {
1349                 return null;
1350             }
1351
1352         }
1353         #endregion
1354
1355         #region 随机数
1356         public static string GetRandNum(int randNumLength)
1357         {
1358             System.Random randNum = new System.Random(unchecked((int)DateTime.Now.Ticks));
1359             StringBuilder sb = new StringBuilder(randNumLength);
1360             for (int i = 0; i < randNumLength; i++)
1361             {
1362                 sb.Append(randNum.Next(0, 9));
1363             }
1364             return sb.ToString();
1365         }
1366
1367         #endregion
1368
1369         #region 过滤HTML代码的函数
1370         /// <summary>
1371         /// 过滤HTML代码的函数
1372         /// </summary>
1373         /// <param name="html"></param>
1374         /// <returns></returns>
1375         public string HtmlStr(string html)
1376       {
1377           System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1378           System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1379           System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1380           System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1381           System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1382           System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1383           System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1384           System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1385           System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
1386           html = regex1.Replace(html, ""); //过滤<script></script>标记
1387           html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
1388
1389
1390           html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
1391           html = regex4.Replace(html, ""); //过滤iframe
1392           html = regex5.Replace(html, ""); //过滤frameset
1393           html = regex6.Replace(html, ""); //过滤frameset
1394           html = regex7.Replace(html, ""); //过滤frameset
1395           html = regex8.Replace(html, ""); //过滤frameset
1396           html = regex9.Replace(html, "");
1397           html = html.Replace(" ", "");
1398
1399
1400           html = html.Replace("</strong>", "");
1401           html = html.Replace("<strong>", "");
1402           return html;
1403
1404         #endregion
1405
1406
1407     }
1408 }
1409

posted @ 2010-12-21 15:37  你妹的sb  阅读(1008)  评论(0编辑  收藏  举报
百度一下