记录下字符串类库,方便今后查阅
主要包含了字符串解决,去除HTML,SQL注入攻击检测,IP地址处理,Cookies操作,根据身份证获取性别、姓名、年龄等等
代码如下:
using System; using System.Collections.Generic; using System.Web; using System.Collections; using System.Text; using System.Text.RegularExpressions; using System.Data; using System.Drawing; using ZXing; using System.Xml; namespace Hplus.Common { public class CommonMethod { public static bool IsPhone(string Phone) { return System.Text.RegularExpressions.Regex.IsMatch(Phone, @"^[1]+[3,5,4,7,8]+\d{9}"); } ////// 判断是否是汉字 /// /// ///public static bool CheckStringChinese(string text) { bool res = false; foreach (char t in text) { if ((int)t > 127) res = true; } return res; } #region 截取字符长度 static string CutString(string str, int len) /// /// 截取字符长度 /// /// 被截取的字符串 /// 所截取的长度 ///子字符串 public static string CutString(string str, int len) { if (str == null || str.Length == 0 || len <= 0) { return string.Empty; } int l = str.Length; #region 计算长度 int clen = 0; while (clen < len && clen < l) { //每遇到一个中文,则将目标长度减一。 if ((int)str[clen] > 128) { len--; } clen++; } #endregion if (clen < l) { return str.Substring(0, clen) + "..."; } else { return str; } } ////// //截取字符串中文 字母 /// /// 源字符串 /// 截取长度! ///public static string SubTrueString(object content, int length) { string strContent = NoHTML(content.ToString()); bool isConvert = false; int splitLength = 0; int currLength = 0; int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); for (int i = 0; i < strContent.Length; i++) { code = Char.ConvertToUtf32(strContent, i); if (code >= chfrom && code <= chend) { currLength += 2; //中文 } else { currLength += 1;//非中文 } splitLength = i + 1; if (currLength >= length) { isConvert = true; break; } } if (isConvert) { return strContent.Substring(0, splitLength); } else { return strContent; } } #endregion #region /*产生验证码*/ GetCode(int codeLength) /// /// 生成一个1到10000000之间的正整数 /// ///public static int GetNums() { int a = new Random().Next(1, 100000000); return a; } /// /// 产生验证码 /// /// 获取的验证码长度 ///验证码 public static string GetCode(int codeLength) { string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] strArr = so.Split(','); string code = ""; Random rand = new Random(); for (int i = 0; i < codeLength; i++) { code += strArr[rand.Next(0, strArr.Length)]; } return code; } ////// 获取一个随机字符串 /// /// ///public static string GetRandomChar(int count) { string[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; StringBuilder sb = new StringBuilder(); Random ran = new Random(); for (int i = 0; i < count; i++) { int temp = ran.Next(s.Length); sb.Append(s[temp]); } return sb.ToString(); } /// /// 获取特定位数的随机数 /// /// ///public static string GetRandomNums(int count) { StringBuilder sb = new StringBuilder(); sb.Append('9', count - 1); int min = int.Parse(sb.ToString()) + 1;//最小值 sb.Append(9); int max = int.Parse(sb.ToString());//最大值 Random ran = new Random(); return ran.Next(min, max).ToString(); } /// /// //获取18位订单编号 /// ///public static string GetOrderNum() { Random ran = new Random(); int random = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); return "order" + dateStr + CovertIntToString(random, 4); } /// /// 获取支付编号 /// ///public static string GetPayNum() { Random ran = new Random(); int random1 = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); int random2 = ran.Next(9999);//四位随机数 return CovertIntToString(random1, 4) + dateStr + CovertIntToString(random2, 4); } public static string GetShopNum() { Random ran = new Random(); int random = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); return "shop-" + dateStr + CovertIntToString(random, 4); } /// /// //获取17位活动编号 /// ///public static string GetHuoDongNum() { Random ran = new Random(); int random = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); return "ACT" + dateStr + CovertIntToString(random, 4); } /// /// //获取购物车产品流水号 /// ///public static string GetCartNum() { Random ran = new Random(); int random = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); return "ddpcart" + dateStr + CovertIntToString(random, 4); } /// /// //获取会员唯一编号 /// ///public static string GetVIPNum() { Random ran = new Random(); int random = ran.Next(9999);//四位随机数 string dateStr = DateTime.Now.ToString("yyyyMMddhhmmss"); return "VIP" + dateStr + CovertIntToString(random, 6); } /// /// 长度不够补0 /// /// /// ///public static string CovertIntToString(object src, int num) { StringBuilder sb = new StringBuilder(); sb.Append('0', num); string sTarget = sb.ToString() + src.ToString(); return sTarget.Substring(sTarget.Length - num, num); } private static double EARTH_RADIUS = 6378137.0; /// /// 获取地球两点之间的距离 /// /// /// /// /// ///public static double Gps2m(double lat_a, double lng_a, double lat_b, double lng_b) { double radLat1 = (lat_a * Math.PI / 180.0); double radLat2 = (lat_b * Math.PI / 180.0); double a = radLat1 - radLat2; double b = (lng_a - lng_b) * Math.PI / 180.0; double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.Round(s * 10000) / 10000; return s; } #endregion #region sql注入攻击 public static string[] words = { "select", "insert", "delete", "count(", "drop table", "update", "truncate", "asc(", "mid(", "char(", "xp_cmdshell", "exec", "master", "net", "and", "or", "where" }; public static string CheckParam(string Value) { Value = Value.Replace("'", ""); Value = Value.Replace(";", ""); Value = Value.Replace("--", ""); Value = Value.Replace("/**/", ""); return Value; } public static string CheckParamThrow(string Value) { for (int i = 0; i < words.Length; i++) { if (Value.IndexOf(words[i], StringComparison.OrdinalIgnoreCase) > 0) { string pattern = string.Format(@"[\W]{0}[\W]", words[i]); Regex rx = new Regex(pattern, RegexOptions.IgnoreCase); if (rx.IsMatch(Value)) throw new Exception("发现sql注入痕迹!"); } } return CheckParam(Value); } /// /// 查找是否含有非法参数 /// /// ///public static bool CheckParamBool(string Value) { for (int i = 0; i < words.Length; i++) { if (Value.IndexOf(words[i], StringComparison.OrdinalIgnoreCase) > 0) return true; } return false; } #endregion #region IP地址处理 /// /// 取得客户端真实IP。如果有代理则取第一个非内网地址 /// by flower.b /// public static string IPAddress { get { string result = String.Empty; result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (result != null && result != String.Empty) { //可能有代理 if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式 result = null; else { if (result.IndexOf(",") != -1) { //有“,”,估计多个代理。取第一个不是内网的IP。 result = result.Replace(" ", "").Replace("'", ""); string[] temparyip = result.Split(",;".ToCharArray()); for (int i = 0; i < temparyip.Length; i++) { if (IsIPAddress(temparyip[i]) && temparyip[i].Substring(0, 3) != "10." && temparyip[i].Substring(0, 7) != "192.168" && temparyip[i].Substring(0, 7) != "172.16.") { return temparyip[i]; //找到不是内网的地址 } } } else if (IsIPAddress(result)) //代理即是IP格式 return result; else result = null; //代理中的内容 非IP,取IP } } string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (null == result || result == String.Empty) result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (result == null || result == String.Empty) result = HttpContext.Current.Request.UserHostAddress; return result; } } ////// 判断是否是IP地址格式 0.0.0.0 /// /// 待判断的IP地址 ///true or false private static bool IsIPAddress(string str1) { if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false; string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$"; Regex regex = new Regex(regformat, RegexOptions.IgnoreCase); return regex.IsMatch(str1); } #endregion #region HTML处理 ////// 将html标签转化为特殊字符type=0或特殊字符转化为HTML type=1 /// /// 源字符串 /// 转化方式 ///public static string HTML_Trans(string vv,int type) { if (type == 0) { vv = vv.Replace(" ", " "); vv = vv.Replace(" ", " "); vv = vv.Replace(">", ">"); vv = vv.Replace("<", "<"); vv = vv.Replace("&", "&"); vv = vv.Replace("\"", """); vv = vv.Replace("'", "&apos"); } if (type == 1) { vv = vv.Replace(" "," "); vv = vv.Replace(" "," "); vv = vv.Replace(">",">"); vv = vv.Replace("<", "<"); vv = vv.Replace("&","&"); vv = vv.Replace(""","\""); vv = vv.Replace("&apos", "'"); } return vv; } /// /// 去掉非法html标签 /// /// ///public static string NoHTML(object html) { if (html == null) return ""; string Htmlstring = html.ToString(); //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"