(转)C# 字符串操作类

001 using System;
0002 using System.Collections.Generic;
0003 using System.Text;
0004 using System.Collections;
0005 using System.Text.RegularExpressions;
0006 using System.Security.Cryptography;
0007 /**/
0008 ////////////////////////////////////////////////////
0009 ///功能:字符文本操作类
0010 ///
0011 ///
0012 ////////////////////////////////////////////////////
0013 namespace XHW
0014 {
0015     /// <summary>
0016     /// 字符文本操作类
0017     /// </summary>
0018     public class StringHelper
0019     {
0020         public static bool IsContains(string[] strs, string value)
0021         {
0022             if (strs == null)
0023             {
0024                 return false;
0025             }
0026   
0027             foreach (string str in strs)
0028             {
0029                 if (str == value)
0030                 {
0031                     return true;
0032                 }
0033             }
0034   
0035             return false;
0036         }
0037   
0038    
0039   
0040         #region 字符串过滤
0041   
0042         #region 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
0043         /**/
0044         /// <summary>
0045         /// 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
0046         /// </summary>
0047         /// <param name="source"></param>
0048         /// <returns></returns>
0049         #endregion
0050         public static string EncodeToHtml(string source)
0051         {
0052             source = source.Trim();
0053             source = source.Replace("'", "''");
0054             source = source.Replace("\\", "\");
0055             source = System.Web.HttpContext.Current.Server.HtmlEncode(source);
0056             source = source.Replace("\r\n", "<br>");
0057             source = source.Replace("\n", "<br>");
0058             return source;
0059         }
0060   
0061   
0062         #region [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码
0063         /**/
0064         /// <summary>
0065         /// [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 (不符合命名规范,请使用 EncodeToHtml 方法 )
0066         /// </summary>
0067         /// <param name="source"></param>
0068         /// <returns></returns>
0069         #endregion
0070         public static string HtmlFilterForInput(string source)
0071         {
0072             return EncodeToHtml(source);
0073         }
0074   
0075   
0076         #region 还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
0077         /**/
0078         /// <summary>
0079         /// 还原HTML编码为字符串,用于返回到input或 Textarea 输入框
0080         /// </summary>
0081         /// <param name="source"></param>
0082         /// <returns></returns>
0083         #endregion
0084         public static string DecodeFormHtml(string source)
0085         {
0086             source = source.Trim();
0087             source = source.Replace("<br>", "\r\n");
0088             source = source.Replace("<br>", "\n");
0089             source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
0090             return source;
0091         }
0092   
0093   
0094         #region [否决的]还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
0095         /**/
0096         /// <summary>
0097         /// [否决的]还原HTML编码为字符串,用于返回到input或 Textarea 输入框 (不符合命名规范,请使用 DecodeFormHtml 方法 )
0098         /// </summary>
0099         /// <param name="source"></param>
0100         /// <returns></returns>
0101         #endregion
0102         public static string DeHtmlFilterForInput(string source)
0103         {
0104             source = source.Trim();
0105             source = source.Replace("<br>", "\r\n");
0106             source = source.Replace("<br>", "\n");
0107             source = System.Web.HttpContext.Current.Server.HtmlDecode(source);
0108             return source;
0109         }
0110   
0111   
0112         #region 检验用户提交的URL参数字符里面是否有非法字符
0113         /**/
0114         /// <summary>
0115         /// 检验用户提交的URL参数字符里面是否有非法字符,如果有则返回True.防止SQL注入.
0116         /// </summary>
0117         /// <param name="str">(string)</param>
0118         /// <returns>bool</returns>
0119         public static bool VerifyString(string str)
0120         {
0121             string strTmp = str.ToUpper();
0122             if (strTmp.IndexOf("SELECT ") >= 0 || strTmp.IndexOf(" AND ") >= 0 || strTmp.IndexOf(" OR ") >= 0 ||
0123                 strTmp.IndexOf("EXEC ") >= 0 || strTmp.IndexOf("CHAR(") >= 0)
0124             {
0125                 return true;
0126             }
0127   
0128             strTmp.Replace("'", "'").Replace(";", ";");
0129             return false;
0130         }
0131   
0132         #endregion
0133   
0134   
0135         #region 过滤 Sql 语句字符串中的注入脚本
0136         /**/
0137         /// <summary>
0138         /// 过滤 Sql 语句字符串中的注入脚本
0139         /// </summary>
0140         /// <param name="source">传入的字符串</param>
0141         /// <returns></returns>
0142         #endregion
0143         public static string FilterSql(string source)
0144         {
0145             //单引号替换成两个单引号
0146             source = source.Replace("'", "''");
0147             source = source.Replace("\"", "“");
0148             source = source.Replace("|", "|");
0149             //半角封号替换为全角封号,防止多语句执行
0150             source = source.Replace(";", ";");
0151   
0152             //半角括号替换为全角括号
0153             source = source.Replace("(", "(");
0154             source = source.Replace(")", ")");
0155   
0156             /**/
0157             ///////////////要用正则表达式替换,防止字母大小写得情况////////////////////
0158   
0159             //去除执行存储过程的命令关键字
0160             source = source.Replace("Exec", "");
0161             source = source.Replace("Execute", "");
0162   
0163             //去除系统存储过程或扩展存储过程关键字
0164             source = source.Replace("xp_", "x p_");
0165             source = source.Replace("sp_", "s p_");
0166   
0167             //防止16进制注入
0168             source = source.Replace("0x", "0 x");
0169   
0170             return source;
0171         }
0172   
0173   
0174         #region [否决的]过滤 Sql 语句字符串中的注入脚本
0175         /**/
0176         /// <summary>
0177         /// [否决的]过滤 Sql 语句字符串中的注入脚本(请使用 FilterSql 方法 )
0178         /// </summary>
0179         /// <param name="source">传入的字符串</param>
0180         /// <returns></returns>
0181         #endregion
0182         public static string SqlFilter(string source)
0183         {
0184             return FilterSql(source);
0185         }
0186   
0187   
0188         #region 过滤字符串只剩数字
0189         /**/
0190         /// <summary>
0191         /// 过滤字符串只剩数字
0192         /// </summary>
0193         /// <param name="source">源字符串</param>
0194         #endregion
0195         public static string FilterToNumber(string source)
0196         {
0197             source = Regex.Replace(source, "[^0-9]*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
0198             return source;
0199         }
0200   
0201   
0202         #region [否决的]过滤字符串只剩数字
0203         /**/
0204         /// <summary>
0205         /// [否决的]过滤字符串只剩数字(请使用 FilterToNumber 方法)
0206         /// </summary>
0207         /// <param name="source">源字符串</param>
0208         #endregion
0209         public static string NumberFilter(string source)
0210         {
0211             source = Regex.Replace(source, "[^0-9]*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
0212             return source;
0213         }
0214   
0215   
0216         #region 去除数组内重复元素
0217         /**/
0218         /// <summary>
0219         /// 去除数组内重复元素
0220         /// </summary>
0221         /// <param name="arr"></param>
0222         /// <returns></returns>
0223         #endregion
0224         public ArrayList FilterRepeatArrayItem(ArrayList arr)
0225         {
0226             //建立新数组
0227             ArrayList newArr = new ArrayList();
0228   
0229             //载入第一个原数组元素
0230             if (arr.Count > 0)
0231             {
0232                 newArr.Add(arr[0]);
0233             }
0234   
0235             //循环比较
0236             for (int i = 0; i < arr.Count; i++)
0237             {
0238                 if (!newArr.Contains(arr[i]))
0239                 {
0240                     newArr.Add(arr[i]);
0241                 }
0242             }
0243             return newArr;
0244         }
0245   
0246   
0247         #region 在最后去除指定的字符
0248         /**/
0249         /// <summary>
0250         /// 在最后去除指定的字符
0251         /// </summary>
0252         /// <param name="source">参加处理的字符</param>
0253         /// <param name="cutString">要去除的字符</param>
0254         /// <returns>返回结果</returns>
0255         #endregion
0256         public static string CutLastString(string source, string cutString)
0257         {
0258             string result = "";
0259             int tempIndex = 0;
0260   
0261             tempIndex = source.LastIndexOf(cutString);
0262             if (cutString.Length == (source.Length - tempIndex))
0263             {
0264                 result = source.Substring(0, tempIndex);
0265             }
0266             else
0267             {
0268                 result = source;
0269             }
0270   
0271             return result;
0272         }
0273   
0274   
0275         #region 利用正则表达式实现UBB代码转换为html代码
0276         /**/
0277         /// <summary>
0278         /// 利用正则表达式实现UBB代码转换为html代码
0279         /// </summary>
0280         /// <param name="source">待处理的文本内容</param>
0281         /// <returns>返回正确的html代码</returns>
0282         #endregion
0283         public static string UBBCode(string source)
0284         {
0285             if (source == null || source.Length == 0)
0286             {
0287                 return "";
0288             }
0289   
0290             source = source.Replace("&nbsp;", "");
0291             //source=source.Replace("<","&lt");
0292             //source=source.Replace(">","&gt");
0293             source = source.Replace("\n", "<br>");
0294             source = Regex.Replace(source, @"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]", @"<a href=$1 target=_blank>$2</a>", RegexOptions.IgnoreCase);
0295             source = Regex.Replace(source, @"\[url\](?<x>[^\]]*)\[/url\]", @"<a href=$1 target=_blank>$1</a>", RegexOptions.IgnoreCase);
0296             source = Regex.Replace(source, @"\[email=(?<x>[^\]]*)\](?<y>[^\]]*)\[/email\]", @"<a href=$1>$2</a>", RegexOptions.IgnoreCase);
0297             source = Regex.Replace(source, @"\[email\](?<x>[^\]]*)\[/email\]", @"<a href=$1>$1</a>", RegexOptions.IgnoreCase);
0298             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);
0299             source = Regex.Replace(source, @"\", @"<IMG src=""$1"" border=0>", RegexOptions.IgnoreCase);
0300             source = Regex.Replace(source, @"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]", @"<font color=$1>$2</font>", RegexOptions.IgnoreCase);
0301             source = Regex.Replace(source, @"\[face=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]", @"<font face=$1>$2</font>", RegexOptions.IgnoreCase);
0302             source = Regex.Replace(source, @"\[size=1\](?<x>[^\]]*)\[/size\]", @"<font size=1>$1</font>", RegexOptions.IgnoreCase);
0303             source = Regex.Replace(source, @"\[size=2\](?<x>[^\]]*)\[/size\]", @"<font size=2>$1</font>", RegexOptions.IgnoreCase);
0304             source = Regex.Replace(source, @"\[size=3\](?<x>[^\]]*)\[/size\]", @"<font size=3>$1</font>", RegexOptions.IgnoreCase);
0305             source = Regex.Replace(source, @"\[size=4\](?<x>[^\]]*)\[/size\]", @"<font size=4>$1</font>", RegexOptions.IgnoreCase);
0306             source = Regex.Replace(source, @"\[size=5\](?<x>[^\]]*)\[/size\]", @"<font size=5>$1</font>", RegexOptions.IgnoreCase);
0307             source = Regex.Replace(source, @"\[size=6\](?<x>[^\]]*)\[/size\]", @"<font size=6>$1</font>", RegexOptions.IgnoreCase);
0308             source = Regex.Replace(source, @"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]", @"<align=$1>$2</align>", RegexOptions.IgnoreCase);
0309             source = Regex.Replace(source, @"\[fly](?<x>[^\]]*)\[/fly]", @"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>", RegexOptions.IgnoreCase);
0310             source = Regex.Replace(source, @"\[move](?<x>[^\]]*)\[/move]", @"<marquee scrollamount=3>$1</marquee>", RegexOptions.IgnoreCase);
0311             source = Regex.Replace(source, @"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]", @"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>", RegexOptions.IgnoreCase);
0312             source = Regex.Replace(source, @"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]", @"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>", RegexOptions.IgnoreCase);
0313             source = Regex.Replace(source, @"\[center\](?<x>[^\]]*)\[/center\]", @"<center>$1</center>", RegexOptions.IgnoreCase);
0314             source = Regex.Replace(source, @"\[b\](?<x>[^\]]*)\[/b\]", @"<b>$1</b>", RegexOptions.IgnoreCase);
0315             source = Regex.Replace(source, @"\[i\](?<x>[^\]]*)\[/i\]", @"<i>$1</i>", RegexOptions.IgnoreCase);
0316             source = Regex.Replace(source, @"\[u\](?<x>[^\]]*)\[/u\]", @"<u>$1</u>", RegexOptions.IgnoreCase);
0317             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);
0318             source = Regex.Replace(source, @"\[list\](?<x>[^\]]*)\[/list\]", @"<ul>$1</ul>", RegexOptions.IgnoreCase);
0319             source = Regex.Replace(source, @"\[list=1\](?<x>[^\]]*)\[/list\]", @"<ol type=1>$1</ol id=1>", RegexOptions.IgnoreCase);
0320             source = Regex.Replace(source, @"\[list=a\](?<x>[^\]]*)\[/list\]", @"<ol type=a>$1</ol id=a>", RegexOptions.IgnoreCase);
0321             source = Regex.Replace(source, @"\[\*\](?<x>[^\]]*)\[/\*\]", @"<li>$1</li>", RegexOptions.IgnoreCase);
0322             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);
0323             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);
0324             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);
0325             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);
0326             return (source);
0327         }
0328   
0329   
0330         #region 整理(过滤)以英文逗号分割的字符串
0331         /**/
0332         /// <summary>
0333         /// 整理(过滤)以英文逗号分割的字符串
0334         /// </summary>
0335         /// <param name="source">原字符串</param>
0336         /// <param name="str2">待清除的字符串,如空格</param>
0337         /// <returns></returns>
0338         #endregion
0339         public static string FilterStringArray(string source, string str2)
0340         {
0341             source = source.Replace(str2, "");
0342             if (source != "")
0343             {
0344                 source = source.Replace(",,", ",");
0345   
0346                 if (source[0].ToString() == ",")
0347                 {
0348                     source = source.Substring(1, source.Length - 1);
0349                 }
0350   
0351                 if (source[source.Length - 1].ToString() == ",")
0352                 {
0353                     source = source.Substring(0, source.Length - 1);
0354                 }
0355             }
0356             return source;
0357         }
0358   
0359   
0360         #endregion
0361   
0362         #region 字符串组合
0363   
0364         #region 返回年月日时分秒组合的字符串
0365         /**/
0366         /// <summary>
0367         /// 返回年月日时分秒组合的字符串,如:20050424143012 
0368         /// </summary>
0369         /// <param name="splitString">中间间隔的字符串,如2005\04\24\14\30\12。可以用来建立目录时使用</param>
0370         /// <returns></returns>
0371         #endregion
0372         public static string GetTimeString()
0373         {
0374             //DateTime now = DateTime.Now;
0375   
0376             //StringBuilder sb = new StringBuilder();
0377             //sb.Append(now.Year.ToString("0000"));
0378             //sb.Append(splitString);
0379             //sb.Append(now.Month.ToString("00"));
0380             //sb.Append(splitString);
0381             //sb.Append(now.Day.ToString("00"));
0382             //sb.Append(splitString);
0383             //sb.Append(now.Hour.ToString("00"));
0384             //sb.Append(splitString);
0385             //sb.Append(now.Minute.ToString("00"));
0386             //sb.Append(splitString);
0387             //sb.Append(now.Second.ToString("00"));
0388           string kk=Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");
0389   
0390           return kk;
0391         }
0392   
0393   
0394         #region 返回年月日时分秒组合的字符串
0395         /**/
0396         /// <summary>
0397         /// 返回年月日组合的字符串,如:20050424 (2005年4月24日)
0398         /// </summary>
0399         /// <param name="splitString">中间间隔的字符串,如2005\04\24 可以用来建立目录时使用</param>
0400         /// <returns></returns>
0401         #endregion
0402         public static string GetDateString()
0403         {
0404             //DateTime now = DateTime.Now;
0405   
0406             //StringBuilder sb = new StringBuilder();
0407             //sb.Append(now.Year.ToString("0000"));
0408             //sb.Append(splitString);
0409             //sb.Append(now.Month.ToString("00"));
0410             //sb.Append(splitString);
0411             //sb.Append(now.Day.ToString("00"));
0412             string kk = Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");
0413             return kk;
0414         }
0415   
0416   
0417         #endregion
0418   
0419         #region 随机字符串,随机数
0420   
0421         private static string _LowerChar = "abcdefghijklmnopqrstuvwxyz";
0422         private static string _UpperChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0423         private static string _NumberChar = "0123456789";
0424   
0425         #region 获取种子
0426         /**/
0427         /// <summary>
0428         /// 使用RNGCryptoServiceProvider 做种,可以在一秒内产生的随机数重复率非常
0429         /// 的低,对于以往使用时间做种的方法是个升级
0430         /// </summary>
0431         /// <returns></returns>
0432         #endregion
0433         public static int GetNewSeed()
0434         {
0435             byte[] rndBytes = new byte[4];
0436             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
0437             rng.GetBytes(rndBytes);
0438             return BitConverter.ToInt32(rndBytes, 0);
0439         }
0440   
0441   
0442         #region 取得指定范围内的数字随几数
0443         /**/
0444         /// <summary>
0445         /// 取得指定范围内的随几数
0446         /// </summary>
0447         /// <param name="startNumber">下限数</param>
0448         /// <param name="endNumber">上限数</param>
0449         /// <returns>int</returns>
0450         #endregion
0451         public static int GetRandomNumber(int startNumber, int endNumber)
0452         {
0453             Random objRandom = new Random(GetNewSeed());
0454             int r = objRandom.Next(startNumber, endNumber);
0455             return r;
0456         }
0457   
0458   
0459         #region 获取指定 ASCII 范围内的随机字符串
0460         /**/
0461         /// <summary>
0462         /// 获取指定 ASCII 范围内的随机字符串
0463         /// </summary>
0464         /// <param name="resultLength">结果字符串长度</param>
0465         /// <param name="startNumber"> 开始的ASCII值 如(33-125)中的 33</param>
0466         /// <param name="endNumber"> 结束的ASCII值 如(33-125)中的 125</param>
0467         /// <returns></returns>
0468         #endregion
0469         public static string GetRandomStringByASCII(int resultLength, int startNumber, int endNumber)
0470         {
0471             System.Random objRandom = new System.Random(GetNewSeed());
0472             string result = null;
0473             for (int i = 0; i < resultLength; i++)
0474             {
0475                 result += (char)objRandom.Next(startNumber, endNumber);
0476             }
0477             return result;
0478         }
0479   
0480   
0481         #region 从指定字符串中抽取指定长度的随机字符串
0482         /**/
0483         /// <summary>
0484         /// 从指定字符串中抽取指定长度的随机字符串
0485         /// </summary>
0486         /// <param name="source">源字符串</param>
0487         /// <param name="resultLength">待获取随机字符串长度</param>
0488         /// <returns></returns>
0489         #endregion
0490         private static string GetRandomString(string source, int resultLength)
0491         {
0492             System.Random objRandom = new System.Random(GetNewSeed());
0493             string result = null;
0494             for (int i = 0; i < resultLength; i++)
0495             {
0496                 result += source.Substring(objRandom.Next(0, source.Length - 1), 1);
0497             }
0498             return result;
0499         }
0500   
0501   
0502         #region 获取指定长度随机的数字字符串
0503         /**/
0504         /// <summary>
0505         /// 获取指定长度随机的数字字符串
0506         /// </summary>
0507         /// <param name="resultLength">待获取随机字符串长度</param>
0508         /// <returns></returns>
0509         #endregion
0510         public static string GetRandomNumberString(int resultLength)
0511         {
0512             return GetRandomString(_NumberChar, resultLength);
0513         }
0514   
0515   
0516         #region 获取指定长度随机的字母字符串(包含大小写字母)
0517         /**/
0518         /// <summary>
0519         /// 获取指定长度随机的字母字符串(包含大小写字母)
0520         /// </summary>
0521         /// <param name="resultLength">待获取随机字符串长度</param>
0522         /// <returns></returns>
0523         #endregion
0524         public static string GetRandomLetterString(int resultLength)
0525         {
0526             return GetRandomString(_LowerChar + _UpperChar, resultLength);
0527         }
0528   
0529   
0530         #region 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
0531         /**/
0532         /// <summary>
0533         /// 获取指定长度随机的字母+数字混和字符串(包含大小写字母)
0534         /// </summary>
0535         /// <param name="resultLength">待获取随机字符串长度</param>
0536         /// <returns></returns>
0537         #endregion
0538         public static string GetRandomMixString(int resultLength)
0539         {
0540             return GetRandomString(_LowerChar + _UpperChar + _NumberChar, resultLength);
0541         }
0542   
0543         #endregion
0544   
0545         #region 字符串验证
0546   
0547         #region 判断字符串是否为整型
0548         /**/
0549         /// <summary>
0550         /// 判断字符串是否为整型
0551         /// </summary>
0552         /// <param name="source"></param>
0553         /// <returns></returns>
0554         #endregion
0555         public static bool IsInteger(string source)
0556         {
0557             if (source == null || source == "")
0558             {
0559                 return false;
0560             }
0561   
0562             if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
0563             {
0564                 return true;
0565             }
0566             else
0567             {
0568                 return false;
0569             }
0570         }
0571   
0572   
0573         #region Email 格式是否合法
0574         /**/
0575         /// <summary>
0576         /// Email 格式是否合法
0577         /// </summary>
0578         /// <param name="strEmail"></param>
0579         #endregion
0580         public static bool IsEmail(string strEmail)
0581         {
0582             return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
0583         }
0584   
0585   
0586         #region 判断是否IP
0587         /**/
0588         /// <summary>
0589         /// 判断是否IP
0590         /// </summary>
0591         /// <param name="source"></param>
0592         /// <returns></returns>
0593         #endregion
0594         public static bool IsIP(string source)
0595         {
0596             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])$");
0597         }
0598   
0599   
0600         #region 检查字符串是否为A-Z、0-9及下划线以内的字符
0601   
0602         /**/
0603         /// <summary>
0604         /// 检查字符串是否为A-Z、0-9及下划线以内的字符
0605         /// </summary>
0606         /// <param name="str">被检查的字符串</param>
0607         /// <returns>是否有特殊字符</returns>
0608         #endregion
0609         public static bool IsLetterOrNumber(string str)
0610         {
0611             bool b = System.Text.RegularExpressions.Regex.IsMatch(str, "[a-zA-Z0-9_]");
0612             return b;
0613         }
0614   
0615   
0616         #region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
0617         /**/
0618         /// <summary>
0619         /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
0620         /// </summary>
0621         /// <param name="source"></param>
0622         /// <returns></returns>
0623         #endregion
0624         public static bool IsSpecialChar(string source)
0625         {
0626             Regex r = new Regex(@"[/\<>:.?*|$]");
0627             return r.IsMatch(source);
0628         }
0629   
0630   
0631         #region 是否全为中文/日文/韩文字符
0632         /**/
0633         /// <summary>
0634         /// 是否全为中文/日文/韩文字符
0635         /// </summary>
0636         /// <param name="source">源字符串</param>
0637         /// <returns></returns>
0638         #endregion
0639         public static bool IsChineseChar(string source)
0640         {
0641             //中文/日文/韩文: [\u4E00-\u9FA5]
0642             //英文:[a-zA-Z]
0643             return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
0644         }
0645   
0646   
0647         #region 是否包含双字节字符(允许有单字节字符)
0648         /**/
0649         /// <summary>
0650         /// 是否包含双字节字符(允许有单字节字符)
0651         /// </summary>
0652         /// <param name="source"></param>
0653         /// <returns></returns>
0654         #endregion
0655         public static bool IsDoubleChar(string source)
0656         {
0657             return Regex.IsMatch(source, @"[^\x00-\xff]");
0658         }
0659   
0660   
0661         #region 是否为日期型字符串
0662         /**/
0663         /// <summary>
0664         /// 是否为日期型字符串
0665         /// </summary>
0666         /// <param name="source">日期字符串(2005-6-30)</param>
0667         /// <returns></returns>
0668         #endregion
0669         public static bool IsDate(string source)
0670         {
0671             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-))$");
0672         }
0673   
0674   
0675         #region 是否为时间型字符串
0676         /**/
0677         /// <summary>
0678         /// 是否为时间型字符串
0679         /// </summary>
0680         /// <param name="source">时间字符串(15:00:00)</param>
0681         /// <returns></returns>
0682         #endregion
0683         public static bool IsTime(string source)
0684         {
0685             return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
0686         }
0687   
0688   
0689         #region 是否为日期+时间型字符串
0690         /**/
0691         /// <summary>
0692         /// 是否为日期+时间型字符串
0693         /// </summary>
0694         /// <param name="source"></param>
0695         /// <returns></returns>
0696         #endregion
0697         public static bool IsDateTime(string source)
0698         {
0699             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)$");
0700         }
0701   
0702   
0703         #region 是否为钱币型数据
0704         /**/
0705         /// <summary>
0706         /// 是否为钱币型数据
0707         /// </summary>
0708         /// <param name="source"></param>
0709         /// <returns></returns>
0710         #endregion
0711         public static bool IsMoney(string source)
0712         {
0713             return false;
0714         }
0715   
0716         #endregion
0717   
0718         #region 字符串截取
0719   
0720         #region 获取字符串的实际长度(按单字节)
0721         /**/
0722         /// <summary>
0723         /// 获取字符串的实际长度(按单字节)
0724         /// </summary>
0725         /// <param name="source"></param>
0726         /// <returns></returns>
0727         #endregion
0728         public static int GetRealLength(string source)
0729         {
0730             return System.Text.Encoding.Default.GetByteCount(source);
0731         }
0732   
0733   
0734         #region 取得固定长度的字符串(按单字节截取)
0735         /**/
0736         /// <summary>
0737         /// 取得固定长度的字符串(按单字节截取)。
0738         /// </summary>
0739         /// <param name="source">源字符串</param>
0740         /// <param name="resultLength">截取长度</param>
0741         /// <returns></returns>
0742         #endregion
0743         public static string SubString(string source, int resultLength)
0744         {
0745   
0746             //判断字符串长度是否大于截断长度
0747             if (System.Text.Encoding.Default.GetByteCount(source) > resultLength)
0748             {
0749                 //判断字串是否为空
0750                 if (source == null)
0751                 {
0752                     return "";
0753                 }
0754   
0755                 //初始化
0756                 int i = 0, j = 0;
0757   
0758                 //为汉字或全脚符号长度加2否则加1
0759                 foreach (char newChar in source)
0760                 {
0761                     if ((int)newChar > 127)
0762                     {
0763                         i += 2;
0764                     }
0765                     else
0766                     {
0767                         i++;
0768                     }
0769                     if (i > resultLength)
0770                     {
0771                         source = source.Substring(0, j);
0772                         break;
0773                     }
0774                     j++;
0775                 }
0776             }
0777             return source;
0778         }
0779   
0780   
0781         #region 按长度分割字符串
0782         /**/
0783         /// <summary>
0784         /// 按长度分割字符串,如短信
0785         /// </summary>
0786         /// <param name="str"></param>
0787         /// <param name="len"></param>
0788         /// <returns></returns>
0789         #endregion
0790         private ArrayList SplitStringByLength(string str, int len)
0791         {
0792             ArrayList arrBlock = new ArrayList();
0793             int intBlockCount = str.Length / len;
0794             if (str.Length % len != 0)
0795             {
0796                 for (int i = 0; i <= intBlockCount; i++)
0797                 {
0798                     if ((str.Length - i * len) > len)
0799                         arrBlock.Add(str.Substring(i * len, len));
0800                     else
0801                         arrBlock.Add(str.Substring(i * len, (str.Length % len)));
0802                 }
0803             }
0804             else
0805             {
0806                 for (int i = 0; i < intBlockCount; i++)
0807                 {
0808                     arrBlock.Add(str.Substring(i * len, len));
0809                 }
0810             }
0811             return arrBlock;
0812         }
0813   
0814   
0815         #endregion
0816   
0817         #region 字符串比较
0818   
0819         /**/
0820         /// <summary>
0821         ///  获得某个字符串在另个字符串中出现的次数
0822         /// </summary>
0823         /// <param name="strOriginal">要处理的字符</param>
0824         /// <param name="strSymbol">符号</param>
0825         /// <returns>返回值</returns>
0826         public static int GetStringIncludeCount(string strOriginal, string strSymbol)
0827         {
0828   
0829             int count = 0;
0830             count = strOriginal.Length - strOriginal.Replace(strSymbol, String.Empty).Length;
0831             return count;
0832         }
0833   
0834         /**/
0835         /// <summary>
0836         /// 获得某个字符串在另个字符串第一次出现时前面所有字符
0837         /// </summary>
0838         /// <param name="strOriginal">要处理的字符</param>
0839         /// <param name="strSymbol">符号</param>
0840         /// <returns>返回值</returns>
0841         public static string GetFirstString(string strOriginal, string strSymbol)
0842         {
0843             int strPlace = strOriginal.IndexOf(strSymbol);
0844             if (strPlace != -1)
0845             {
0846                 strOriginal = strOriginal.Substring(0, strPlace);
0847             }
0848             return strOriginal;
0849         }
0850   
0851         /**/
0852         /// <summary>
0853         /// 获得某个字符串在另个字符串最后一次出现时后面所有字符
0854         /// </summary>
0855         /// <param name="strOriginal">要处理的字符</param>
0856         /// <param name="strSymbol">符号</param>
0857         /// <returns>返回值</returns>
0858         public static string GetLastString(string strOriginal, string strSymbol)
0859         {
0860             int strPlace = strOriginal.LastIndexOf(strSymbol) + strSymbol.Length;
0861             strOriginal = strOriginal.Substring(strPlace);
0862             return strOriginal;
0863         }
0864   
0865         /**/
0866         /// <summary>
0867         /// 获得两个字符之间第一次出现时前面所有字符
0868         /// </summary>
0869         /// <param name="strOriginal">要处理的字符</param>
0870         /// <param name="strFirst">最前哪个字符</param>
0871         /// <param name="strLast">最后哪个字符</param>
0872         /// <returns>返回值</returns>
0873         public static string GetTwoMiddleFirstStr(string strOriginal, string strFirst, string strLast)
0874         {
0875             strOriginal = GetFirstString(strOriginal, strLast);
0876             strOriginal = GetLastString(strOriginal, strFirst);
0877             return strOriginal;
0878         }
0879   
0880         /**/
0881         /// <summary>
0882         ///  获得两个字符之间最后一次出现时的所有字符
0883         /// </summary>
0884         /// <param name="strOriginal">要处理的字符</param>
0885         /// <param name="strFirst">最前哪个字符</param>
0886         /// <param name="strLast">最后哪个字符</param>
0887         /// <returns>返回值</returns>
0888         public static string GetTwoMiddleLastStr(string strOriginal, string strFirst, string strLast)
0889         {
0890             strOriginal = GetLastString(strOriginal, strFirst);
0891             strOriginal = GetFirstString(strOriginal, strLast);
0892             return strOriginal;
0893         }
0894   
0895         /**/
0896         /// <summary>
0897         /// 发帖过滤词(用“|”号分隔)Application["app_state_FilterWord"]
0898         /// </summary>
0899         /// <param name="str">字符串</param>
0900         /// <param name="chkword">过滤词(用“|”号分隔)</param>
0901         public static bool CheckBadWords(string str, string chkword)
0902         {
0903             if (chkword != null && chkword != "")
0904             {
0905                 string filter = chkword;
0906                 string chk1 = "";
0907                 string[] aryfilter = filter.Split('|');
0908                 for (int i = 0; i < aryfilter.Length; i++)
0909                 {
0910                     chk1 = aryfilter[i].ToString();
0911                     if (str.IndexOf(chk1) >= 0)
0912                         return true;
0913                 }
0914             }
0915             return false;
0916         }
0917   
0918         /**/
0919         /// <summary>
0920         /// 发帖过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)Application["app_state_Check_FilterWord"]
0921         /// </summary>
0922         /// <param name="str">字符串</param>
0923         /// <param name="chkword">过滤字(需审核)(不同组间用“§”号分隔,同组内用“,”分隔)</param>
0924         public static bool CheckilterStr(string str, string chkword)
0925         {
0926   
0927             if (chkword != null && chkword != "")
0928             {
0929                 string filter = chkword;
0930                 string[] aryfilter = filter.Split('§');
0931                 string[] aryfilter_lei;
0932                 int lei_for = 0, j;
0933   
0934                 for (int i = 0; i < aryfilter.Length; i++)
0935                 {
0936                     lei_for = 0;
0937                     aryfilter_lei = aryfilter[i].Split(',');
0938                     for (j = 0; j < aryfilter_lei.Length; j++)
0939                     {
0940                         if (str.IndexOf(aryfilter_lei[j].ToString()) >= 0)
0941                             lei_for += 1;
0942                     }
0943                     if (lei_for == aryfilter_lei.Length)
0944                         return true;
0945                 }
0946             }
0947             return false;
0948         }
0949   
0950   
0951         #endregion
0952   
0953         #region 字符集转换
0954   
0955         #region 将 GB2312 值转换为 UTF8 字符串(如:测试 -> 娴嬭瘯 )
0956         /**/
0957         /// <summary>
0958         /// 将 GB2312 值转换为 UTF8 字符串(如:测试 -> 娴嬭瘯 )
0959         /// </summary>
0960         /// <param name="source"></param>
0961         /// <returns></returns>
0962         #endregion
0963         public static string ConvertGBToUTF8(string source)
0964         {
0965             /**/
0966             /*
0967         byte[] bytes;
0968         bytes = System.Text.Encoding.Default.GetBytes(source);
0969         System.Text.Decoder gbDecoder = System.Text.Encoding.GetEncoding("gb2312").GetDecoder();
0970         int charCount = gbDecoder.GetCharCount(bytes,0 , bytes.Length);
0971         Char[] chars = new Char[charCount];
0972         int charsDecodedCount = gbDecoder.GetChars(bytes,0,bytes.Length,chars,0);
0973         return new string(chars);
0974         */
0975   
0976             return Encoding.GetEncoding("GB2312").GetString(Encoding.UTF8.GetBytes(source));
0977         }
0978   
0979   
0980         #region 将 UTF8 值转换为 GB2312 字符串 (如:娴嬭瘯 -> 测试)
0981         /**/
0982         /// <summary>
0983         /// 将 UTF8 值转换为 GB2312 字符串 (如:娴嬭瘯 -> 测试)
0984         /// </summary>
0985         /// <param name="source"></param>
0986         /// <returns></returns>
0987         #endregion
0988         public static string ConvertUTF8ToGB(string source)
0989         {
0990             /**/
0991             /*
0992         byte[] bytes;
0993         //模拟编码
0994         System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
0995         bytes = utf8.GetBytes(source); 
0996               
0997         //开始解码
0998         System.Text.Decoder utf8Decoder = System.Text.Encoding.UTF8.GetDecoder();
0999   
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 }
posted @ 2011-02-07 20:59  fsl  阅读(1088)  评论(0编辑  收藏  举报