1 /// <summary> 2 /// 字符帮助类 3 /// </summary> 4 public class StringHelper 5 { 6 private static readonly Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$", RegexOptions.IgnoreCase); //验证邮箱正则 7 private static readonly Regex RegHasCHZN = new Regex("[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);//验证是否含中文正则 8 private static readonly Regex RegCHZN = new Regex(@"^[\u4e00-\u9fa5]{1,}$", RegexOptions.IgnoreCase);//验证是否为中文正则 9 private static readonly Regex RegPhone = new Regex("(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)", RegexOptions.IgnoreCase); //验证电话和手机号码正则 10 private static readonly Regex RegUrl = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?",RegexOptions.IgnoreCase);//验证Url正则 11 private static readonly Regex RegNumber = new Regex(@"^[0-9]*$", RegexOptions.IgnoreCase);//验证纯数字l正则 12 13 #region 判断IP格式 14 15 /// <summary> 16 /// 判断是否是IP地址格式 0.0.0.0 17 /// </summary> 18 /// <param name="input">待判断的IP地址</param> 19 /// <returns>true or false</returns> 20 public static bool IsIPAddress(string input) 21 { 22 if (input.IsNullOrEmpty() || input.Length < 7 || input.Length > 15) return false; 23 24 const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$"; 25 26 var regex = new Regex(regFormat, RegexOptions.IgnoreCase); 27 return regex.IsMatch(input); 28 } 29 30 #endregion 31 32 #region 判断邮箱格式 33 34 /// <summary> 35 /// 判断邮箱格式 36 /// </summary> 37 /// <param name="input">文本信息</param> 38 /// <returns>验证结果</returns> 39 public static bool IsEmail(string input) 40 { 41 if (input.IsNullOrEmpty()) 42 return false; 43 Match m = RegEmail.Match(input); 44 return m.Success; 45 } 46 #endregion 47 48 #region 中文字符检测 49 50 /// <summary> 51 /// 检测是否有中文字符 52 /// </summary> 53 /// <param name="input">文本信息</param> 54 /// <returns>检测结果</returns> 55 public static bool IsContainCHZN(string input) 56 { 57 if (input.IsNullOrEmpty()) 58 return false; 59 Match m = RegHasCHZN.Match(input); 60 return m.Success; 61 } 62 #endregion 63 64 #region 判断是否中文 65 /// <summary> 66 /// 判断参数是否为中文字符 67 /// </summary> 68 /// <param name="input">文本信息</param> 69 /// <returns></returns> 70 public static bool IsChinese(string input) 71 { 72 if (input.IsNullOrEmpty()) 73 return false; 74 Match m = RegCHZN.Match(input); 75 return m.Success; 76 } 77 #endregion 78 79 #region 判断电话号码 80 // 电话号码和手机号码检查 81 /// <summary> 82 /// 电话号码和手机号码检查 83 /// </summary> 84 /// <param name="input">电话号码或手机号码</param> 85 /// <returns>匹配结果</returns> 86 public static bool IsPhone(string input) 87 { 88 if (input.IsNullOrEmpty()) 89 return false; 90 Match m = RegPhone.Match(input); 91 return m.Success; 92 } 93 #endregion 94 95 #region 判断Url合法性 96 /// <summary> 97 /// 验证请求的url是否合法 98 /// </summary> 99 /// <param name="input">输入参数</param> 100 /// <returns></returns> 101 public static bool IsUrl(string input) 102 { 103 if (input.IsNullOrEmpty()) 104 return false; 105 Match m = RegUrl.Match(input); 106 return m.Success; 107 } 108 #endregion 109 110 #region 判断是否数字 111 /// <summary> 112 /// 验证参数是否为数字 113 /// </summary> 114 /// <param name="input">输入字符串</param> 115 /// <returns></returns> 116 public static bool IsNumberic(string input) 117 { 118 if (input.IsNullOrEmpty()) 119 return false; 120 Match m = RegNumber.Match(input); 121 return m.Success; 122 } 123 #endregion 124 125 #region 字符串截取 126 /// <summary> 127 /// 检查字符串最大长度,返回指定长度的串 128 /// </summary> 129 /// <param name="input">输入字符串</param> 130 /// <param name="maxLength">最大长度</param> 131 /// <returns></returns> 132 public static string TextSbustring(string input, int maxLength) 133 { 134 if (input.IsNullOrEmpty()) 135 return null; 136 input = input.Trim(); 137 if (input.Length > maxLength)//按最大长度截取字符串 138 input = input.Substring(0, maxLength); 139 return input; 140 } 141 #endregion 142 143 #region 判断是否含关键词 144 /// <summary> 145 /// 检查是否包含SQL关键字 146 /// </summary> 147 /// <param name="input">被检查的字符串</param> 148 /// <returns>存在SQL关键字返回true,不存在返回false</returns> 149 public static bool IsContainSQLKeyWord(string input) 150 { 151 if (input.IsNullOrEmpty()) 152 return false; 153 string strKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and"; 154 string strRegex = @"[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']"; 155 if (Regex.IsMatch(input, strKeyWord, RegexOptions.IgnoreCase) || Regex.IsMatch(input, strRegex)) 156 return true; 157 return false; 158 } 159 160 /// <summary> 161 /// 检查是否含有关键字 162 /// </summary> 163 /// <param name="input">被检查的字符串</param> 164 /// <returns></returns> 165 public static bool IsContainKeywords(string input) 166 { 167 if (input.IsNullOrEmpty()) 168 return false; 169 string sLowerStr = input.ToLower(); 170 string sRxStr = @"(\sand\s)|(\sand\s)|(\slike\s)|(select\s)|(insert\s)|(delete\s)|(update\s[\s\S].*\sset)|(create\s)|(\stable)|(<[iframe|/iframe|script|/script])|(')|(\sexec)|(declare)|(\struncate)|(\smaster)|(\sbackup)|(\smid)|(\scount)|(cast)|(%)|(\sadd\s)|(\salter\s)|(\sdrop\s)|(\sfrom\s)|(\struncate\s)|(\sxp_cmdshell\s)"; 171 Regex sRx = new Regex(sRxStr); 172 return sRx.IsMatch(sLowerStr, 0); 173 } 174 #endregion 175 176 #region 判断是否包含空格 177 /// <summary> 178 /// 检查是否包含空格 179 /// </summary> 180 /// <param name="input">被检查的字符串</param> 181 /// <returns>存在空格返回true,不存在返回false</returns> 182 public static bool IsContainSpace(string input) 183 { 184 if (input.IsNullOrEmpty()) 185 return false; 186 if (input.IndexOf(" ") >= 0) 187 return true; 188 else 189 return false; 190 } 191 #endregion 192 }
/// <summary> /// 将对象转化为Int32类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static int GetInt(this object input) { return Convert.ToInt32(input); } /// <summary> /// 将对象转化为Int64类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static long GetLong(this object input) { return Convert.ToInt64(input); } /// <summary> /// 将对象转化为DateTime类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static DateTime GetDateTime(this object input) { return Convert.ToDateTime(input); } /// <summary> /// 将对象转化为DateTime类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static DateTime? GetDateTimeNullable(this object input) { if (input == null||input.ToString().IsNullOrEmpty()) return null; return Convert.ToDateTime(input); } /// <summary> /// 将对象转化为Decimal类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static Decimal GetDecimal(this object input) { return Convert.ToDecimal(input); } /// <summary> /// 将对象转化为Boolean类型 /// </summary> /// <param name="input"></param> /// <returns></returns> public static bool GetBool(this object input) { return Convert.ToBoolean(input); }
/// <summary> /// 字符串是否为空 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsNullOrEmpty(this string str) { return str == null || str == string.Empty || str == ""; } /// <summary> /// 字符串是否不为空 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsNotNullOrEmpty(this string str) { return str != null && str != string.Empty && str != ""; } /// <summary> /// 判断是否是大于或等于0的整数 /// </summary> /// <param name="input"></param> /// <returns></returns> public static bool IsNumeric(this string input) { int output = 0; return int.TryParse(input, out output); } /// <summary> /// 判断是否是合法日期 /// </summary> /// <param name="input"></param> /// <returns></returns> public static bool IsDateTime(this string input) { DateTime tmp; return DateTime.TryParse(input, out tmp); } /// <summary> /// 判断字符串是否是Decimal /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool IsDecimal(this string input) { Decimal output = 0; return decimal.TryParse(input, out output); } /// <summary> /// 判断对象是否是Boolean /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool IsBoolean(this string input) { bool output = false; return Boolean.TryParse(input, out output); }