.net工具类——类型判断
.net工具类——类型判断
- IsDateTime
- IsNumeric
- IsInt
- IsDouble
- IsDecimal
- IsValidEmail
- IsValidDoEmail
- IsURL
#region 类型判断 /// <summary> /// 判断对象是否为DateTime类型的日期 /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static bool IsDateTime(string expression) { DateTime dt; if (DateTime.TryParse(expression, out dt)) { return true; } return false; } /// <summary> /// 判断对象是否为Int32类型的数字 /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static bool IsNumeric(object expression) { if (expression != null) return IsInt(expression.ToString()); return false; } /// <summary> /// 判断对象是否为Int32类型的数字 /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static bool IsInt(string expression) { if (expression != null) { int val; if (int.TryParse(expression, out val)) { return true; } } return false; } /// <summary> /// 是否为double类型 /// </summary> /// <param name="expression"></param> /// <returns></returns> public static bool IsDouble(string expression) { if (expression != null) { double val; if (double.TryParse(expression, out val)) { return true; } } return false; } /// <summary> /// 是否为decimal类型 /// </summary> /// <param name="expression"></param> /// <returns></returns> public static bool IsDecimal(string expression) { if (expression != null) { decimal val; if (decimal.TryParse(expression, out val)) { return true; } } return false; } /// <summary> /// 检测是否符合email格式 /// </summary> /// <param name="strEmail">要判断的email字符串</param> /// <returns>判断结果</returns> public static bool IsValidEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]"); } public static bool IsValidDoEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } /// <summary> /// 检测是否是正确的Url /// </summary> /// <param name="strUrl">要验证的Url</param> /// <returns>判断结果</returns> public static bool IsURL(string strUrl) { return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); } #endregion 类型判断