asp.net很有用的字符串操作类(转载)

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
     
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 ///
16 /// 字符文本操作类
17 ///

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 ///
45 /// 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
46 ///

47 ///
48 ///
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 " , "
" );
57 source = source.Replace( " \n " , "
" );
58 return source;
59 }
60
61
62 #region [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
63 /**/
64 ///
65 /// [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 (不符合命名规范,请使用 EncodeToHtml 方法 )
66 ///

67 ///
68 ///
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 ///
79 /// 还原HTML编码为字符串,用于返回到input或 Textarea 输入框
80 ///

81 ///
82 ///
83 #endregion
84 public static string DecodeFormHtml( string source)
85 {
86 source = source.Trim();
87 source = source.Replace( "
" , " \r\n " );
88 source = source.Replace( "
" , " \n " );
89 source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
90 return source;
91 }
92
93
94 #region [否决的]还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
95 /**/
96 ///
97 /// [否决的]还原HTML编码为字符串,用于返回到input或 Textarea 输入框 (不符合命名规范,请使用 DecodeFormHtml 方法 )
98 ///

99 ///
100 ///
101 #endregion
102 public static string DeHtmlFilterForInput( string source)
103 {
104 source = source.Trim();
105 source = source.Replace( "
" , " \r\n " );
106 source = source.Replace( "
" , " \n " );
107 source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
108 return source;
109 }
110
111
112 #region 检验用户提交的URL参数字符里面是否有非法字符
113 /**/
114 ///
115 /// 检验用户提交的URL参数字符里面是否有非法字符,如果有则返回True.防止SQL注入.
116 ///

117 /// (string)
118 /// bool
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 ///
138 /// 过滤 Sql 语句字符串中的注入脚本
139 ///

140 /// 传入的字符串
141 ///
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 ///
177 /// [否决的]过滤 Sql 语句字符串中的注入脚本(请使用 FilterSql 方法 )
178 ///

179 /// 传入的字符串
180 ///
181 #endregion
182 public static string SqlFilter( string source)
183 {
184 return FilterSql(source);
185 }
186
187
188 #region 过滤字符串只剩数字
189 /**/
190 ///
191 /// 过滤字符串只剩数字
192 ///

193 /// 源字符串
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 ///
205 /// [否决的]过滤字符串只剩数字(请使用 FilterToNumber 方法)
206 ///

207 /// 源字符串
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 ///
219 /// 去除数组内重复元素
220 ///

221 ///
222 ///
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 ///
250 /// 在最后去除指定的字符
251 ///

252 /// 参加处理的字符
253 /// 要去除的字符
254 /// 返回结果
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 ///
278 /// 利用正则表达式实现UBB代码转换为html代码
279 ///

280 /// 待处理的文本内容
281 /// 返回正确的html代码
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( "   " , " " );
291 // source=source.Replace("<","<");
292 // source=source.Replace(">",">");
293 source = source.Replace( " \n " , "
" );
294 source = Regex.Replace(source, @" \[url=(?[^\]]*)\](?[^\]]*)\[/url\] " , @" $2 " , RegexOptions.IgnoreCase);
295 source = Regex.Replace(source, @" \[url\](?[^\]]*)\[/url\] " , @" $1 " , RegexOptions.IgnoreCase);
296 source = Regex.Replace(source, @" \[email=(?[^\]]*)\](?[^\]]*)\[/email\] " , @" $2 " , RegexOptions.IgnoreCase);
297 source = Regex.Replace(source, @" \[email\](?[^\]]*)\[/email\] " , @" $1 " , RegexOptions.IgnoreCase);
298 source = Regex.Replace(source, @" \[flash](?[^\]]*)\[/flash] " , @" $1 " , RegexOptions.IgnoreCase);
299 source = Regex.Replace(source, @" \ " , @" " , RegexOptions.IgnoreCase);
300 source = Regex.Replace(source, @" \[color=(?[^\]]*)\](?[^\]]*)\[/color\] " , @" $2 " , RegexOptions.IgnoreCase);
301 source = Regex.Replace(source, @" \[face=(?[^\]]*)\](?[^\]]*)\[/face\] " , @" $2 " , RegexOptions.IgnoreCase);
302 source = Regex.Replace(source, @" \[size=1\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
303 source = Regex.Replace(source, @" \[size=2\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
304 source = Regex.Replace(source, @" \[size=3\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
305 source = Regex.Replace(source, @" \[size=4\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
306 source = Regex.Replace(source, @" \[size=5\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
307 source = Regex.Replace(source, @" \[size=6\](?[^\]]*)\[/size\] " , @" $1 " , RegexOptions.IgnoreCase);
308 source = Regex.Replace(source, @" \[align=(?[^\]]*)\](?[^\]]*)\[/align\] " , @" $2 " , RegexOptions.IgnoreCase);
309 source = Regex.Replace(source, @" \[fly](?[^\]]*)\[/fly] " , @" $1 " , RegexOptions.IgnoreCase);
310 source = Regex.Replace(source, @" \[move](?[^\]]*)\[/move] " , @" $1 " , RegexOptions.IgnoreCase);
311 source = Regex.Replace(source, @" \[glow=(?[^\]]*),(?[^\]]*),(?[^\]]*)\](?[^\]]*)\[/glow\] " , @" $4
" , RegexOptions.IgnoreCase);
312 source = Regex.Replace(source, @" \[shadow=(?[^\]]*),(?[^\]]*),(?[^\]]*)\](?[^\]]*)\[/shadow\] " , @" $4
" , RegexOptions.IgnoreCase);
313 source = Regex.Replace(source, @" \[center\](?[^\]]*)\[/center\] " , @"
$1
" , RegexOptions.IgnoreCase);
314 source = Regex.Replace(source, @" \[b\](?[^\]]*)\[/b\] " , @" $1 " , RegexOptions.IgnoreCase);
315 source = Regex.Replace(source, @" \[i\](?[^\]]*)\[/i\] " , @" $1 " , RegexOptions.IgnoreCase);
316 source = Regex.Replace(source, @" \[u\](?[^\]]*)\[/u\] " , @" $1 " , RegexOptions.IgnoreCase);
317 source = Regex.Replace(source, @" \[code\](?[^\]]*)\[/code\] " , @"
$1
" , RegexOptions.IgnoreCase);
318 source = Regex.Replace(source, @" \[list\](?[^\]]*)\[/list\] " , @"
    $1
" , RegexOptions.IgnoreCase);
319 source = Regex.Replace(source, @" \[list=1\](?[^\]]*)\[/list\] " , @"
    $1
" , RegexOptions.IgnoreCase);
320 source = Regex.Replace(source, @" \[list=a\](?[^\]]*)\[/list\] " , @"
    $1
" , RegexOptions.IgnoreCase);
321 source = Regex.Replace(source, @" \[\*\](?[^\]]*)\[/\*\] " , @"
  • $1
  • " , RegexOptions.IgnoreCase);
    322 source = Regex.Replace(source, @" \[quote](?.*)\[/quote] " , @"
    —— 以下是引用 ——
    $1
    " , RegexOptions.IgnoreCase);
    323 source = Regex.Replace(source, @" \[QT=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/QT] " , @" " , RegexOptions.IgnoreCase);
    324 source = Regex.Replace(source, @" \[MP=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/MP] " , @" " , RegexOptions.IgnoreCase);
    325 source = Regex.Replace(source, @" \[RM=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/RM] " , @"
    " , RegexOptions.IgnoreCase);
    326 return (source);
    327 }
    328
    329
    330 #region 整理(过滤)以英文逗号分割的字符串
    331 /**/
    332 ///
    333 /// 整理(过滤)以英文逗号分割的字符串
    334 ///

    335 /// 原字符串
    336 /// 待清除的字符串,如空格
    337 ///
    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 ///
    367 /// 返回年月日时分秒组合的字符串,如:20050424143012
    368 ///

    369 /// 中间间隔的字符串,如2005\04\24\14\30\12。可以用来建立目录时使用
    370 ///
    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 ///
    397 /// 返回年月日组合的字符串,如:20050424 (2005年4月24日)
    398 ///

    399 /// 中间间隔的字符串,如2005\04\24 可以用来建立目录时使用
    400 ///
    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 ///
    428 /// 使用RNGCryptoServiceProvider 做种,可以在一秒内产生的随机数重复率非常
    429 /// 的低,对于以往使用时间做种的方法是个升级
    430 ///

    431 ///
    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 ///
    445 /// 取得指定范围内的随几数
    446 ///

    447 /// 下限数
    448 /// 上限数
    449 /// int
    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 ///
    462 /// 获取指定 ASCII 范围内的随机字符串
    463 ///

    464 /// 结果字符串长度
    465 /// 开始的ASCII值 如(33-125)中的 33
    466 /// 结束的ASCII值 如(33-125)中的 125
    467 ///
    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 ///
    484 /// 从指定字符串中抽取指定长度的随机字符串
    485 ///

    486 /// 源字符串
    487 /// 待获取随机字符串长度
    488 ///
    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 ///
    505 /// 获取指定长度随机的数字字符串
    506 ///

    507 /// 待获取随机字符串长度
    508 ///
    509 #endregion
    510 public static string GetRandomNumberString( int resultLength)
    511 {
    512 return GetRandomString(_NumberChar, resultLength);
    513 }
    514
    515
    516 #region 获取指定长度随机的字母字符串(包含大小写字母)
    517 /**/
    518 ///
    519 /// 获取指定长度随机的字母字符串(包含大小写字母)
    520 ///

    521 /// 待获取随机字符串长度
    522 ///
    523 #endregion
    524 public static string GetRandomLetterString( int resultLength)
    525 {
    526 return GetRandomString(_LowerChar + _UpperChar, resultLength);
    527 }
    528
    529
    530 #region 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
    531 /**/
    532 ///
    533 /// 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
    534 ///

    535 /// 待获取随机字符串长度
    536 ///
    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 ///
    550 /// 判断字符串是否为整型
    551 ///

    552 ///
    553 ///
    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 ///
    576 /// Email 格式是否合法
    577 ///

    578 ///
    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 ///
    589 /// 判断是否IP
    590 ///

    591 ///
    592 ///
    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 ///
    604 /// 检查字符串是否为A-Z、0-9及下划线以内的字符
    605 ///

    606 /// 被检查的字符串
    607 /// 是否有特殊字符
    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 ///
    619 /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
    620 ///

    621 ///
    622 ///
    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 ///
    634 /// 是否全为中文/日文/韩文字符
    635 ///

    636 /// 源字符串
    637 ///
    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 ///
    650 /// 是否包含双字节字符(允许有单字节字符)
    651 ///

    652 ///
    653 ///
    654 #endregion
    655 public static bool IsDoubleChar( string source)
    656 {
    657 return Regex.IsMatch(source, @" [^\x00-\xff] " );
    658 }
    659
    660
    661 #region 是否为日期型字符串
    662 /**/
    663 ///
    664 /// 是否为日期型字符串
    665 ///

    666 /// 日期字符串(2005-6-30)
    667 ///
    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 ///
    678 /// 是否为时间型字符串
    679 ///

    680 /// 时间字符串(15:00:00)
    681 ///
    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 ///
    692 /// 是否为日期+时间型字符串
    693 ///

    694 ///
    695 ///
    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 ///
    706 /// 是否为钱币型数据
    707 ///

    708 ///
    709 ///
    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 ///
    723 /// 获取字符串的实际长度(按单字节)
    724 ///

    725 ///
    726 ///
    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 ///
    737 /// 取得固定长度的字符串(按单字节截取)。
    738 ///

    739 /// 源字符串
    740 /// 截取长度
    741 ///
    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 ///
    784 /// 按长度分割字符串,如短信
    785 ///

    786 ///
    787 ///
    788 ///
    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 ///
    821 /// 获得某个字符串在另个字符串中出现的次数
    822 ///

    823 /// 要处理的字符
    824 /// 符号
    825 /// 返回值
    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 ///
    836 /// 获得某个字符串在另个字符串第一次出现时前面所有字符
    837 ///

    838 /// 要处理的字符
    839 /// 符号
    840 /// 返回值
    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 ///
    853 /// 获得某个字符串在另个字符串最后一次出现时后面所有字符
    854 ///

    855 /// 要处理的字符
    856 /// 符号
    857 /// 返回值
    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 ///
    867 /// 获得两个字符之间第一次出现时前面所有字符
    868 ///

    869 /// 要处理的字符
    870 /// 最前哪个字符
    871 /// 最后哪个字符
    872 /// 返回值
    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 ///
    882 /// 获得两个字符之间最后一次出现时的所有字符
    883 ///

    884 /// 要处理的字符
    885 /// 最前哪个字符
    886 /// 最后哪个字符
    887 /// 返回值
    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 ///
    897 /// 发帖过滤词(用“|”号分隔)Application["app_state_FilterWord"]
    898 ///

    899 /// 字符串
    900 /// 过滤词(用“|”号分隔)
    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 ///
    920 /// 发帖过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)Application["app_state_Check_FilterWord"]
    921 ///

    922 /// 字符串
    923 /// 过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)
    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 ///
    958 /// 将 GB2312 值转换为 UTF8 字符串(如:测试 -> 娴嬭瘯 )
    959 ///

    960 ///
    961 ///
    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 ///
    983 /// 将 UTF8 值转换为 GB2312 字符串 (如:娴嬭瘯 -> 测试)
    984 ///

    985 ///
    986 ///
    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 ///
    1014 /// 由16进制转为汉字字符串(如:B2E2 -> 测 )
    1015 ///

    1016 ///
    1017 ///
    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 ///
    1034 /// 字符串转为16进制字符串(如:测 -> B2E2 )
    1035 ///

    1036 ///
    1037 ///
    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 ///
    1070 /// 字符串转为unicode字符串(如:测试 -> 测试)
    1071 ///

    1072 ///
    1073 ///
    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 ///
    1100 /// 字符串转为UTF8字符串(如:测试 -> \u6d4b\u8bd5)
    1101 ///

    1102 ///
    1103 ///
    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 ///
    1147 /// 将字符串使用base64算法加密
    1148 ///

    1149 /// 待加密的字符串
    1150 /// 加码后的文本字符串
    1151 public static string Base64_Encrypt( string source)
    1152 {
    1153 return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(source));
    1154 }
    1155
    1156 /**/
    1157 ///
    1158 /// 从Base64编码的字符串中还原字符串,支持中文
    1159 ///

    1160 /// Base64加密后的字符串
    1161 /// 还原后的文本字符串
    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 ///
    1175 /// 将字符串反转
    1176 ///

    1177 ///
    1178 ///
    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 ///
    1191 /// 格式化为小数点两位的字符串(四舍五入)
    1192 ///

    1193 ///
    1194 ///
    1195 public static string FormatFloat2( double source)
    1196 {
    1197 // double tmp=1223333.24215265652;
    1198 return source.ToString( " ##,###.000 " );
    1199 }
    1200
    1201
    1202 /**/
    1203 ///
    1204 /// 将数字字符串转成货币格式
    1205 ///

    1206 ///
    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 ///
    1236 /// 格式化字符串 字符串1234567,想变成1,234,567
    1237 ///

    1238 ///
    1239 ///
    1240 public static string GetO1( string source)
    1241 {
    1242 // return source.ToString("N");
    1243 // return source.ToString("###,###");
    1244 return "" ;
    1245 }
    1246
    1247
    1248 /**/
    1249 ///
    1250 /// 十进制数字1转换为字符串0a,在前面补零
    1251 ///

    1252 ///
    1253 ///
    1254 public static string OctToHex( int source)
    1255 {
    1256 return source.ToString( " X2 " );
    1257 }
    1258
    1259
    1260 /**/
    1261 ///
    1262 /// 将一个十六进制表示的字符串转成int型?如"0A" = 10
    1263 ///

    1264 ///
    1265 ///
    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 ///
    1279 /// 从18位身份证获取用户所在省份、生日、性别信息
    1280 ///

    1281 /// 身份证字符串
    1282 /// 如:福建,1978-06-30,男
    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 ///
    1324 /// 十五位的身份证号转为十八位的
    1325 ///

    1326 /// 十五位的身份证号
    1327 ///
    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 ///
    1371 /// 过滤HTML代码的函数
    1372 ///

    1373 ///
    1374 ///
    1375 public string HtmlStr( string html)
    1376 {
    1377 System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex( @" " , 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( @" " , System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    1381 System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex( @" " , System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    1382 System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex( @" \]+\> " , System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    1383 System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex( @"

    " , System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    1384 System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex( @"

    " , 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, "" ); // 过滤标记
    1387 html = regex2.Replace(html, "" ); // 过滤href=javascript: () 属性
    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( " " , "" );
    1401 html = html.Replace( " " , "" );
    1402 return html;
    1403 }
    1404 #endregion
    1405
    1406
    1407 }
    1408 }
    1409

     

    转载于:https://www.cnblogs.com/xuwb/archive/2010/06/26/1765652.html

    你可能感兴趣的:(javascript,ViewUI)