功能
1.判断一个字符串是否为null或者空字符串
2.判断一个字符串是否为null或者空白字符
3.判断一个字符串是否为数字
4.判断一个字符串是否为邮件
5.判断一个字符串是否为字母加数字
6.判断一个字符串是否为手机号码
7.判断一个字符串是否为电话号码
8.判断一个字符串是否为网址
9.判断一个字符串是否为IP地址
10.判断是否为字母
转换
11.string 转 int
12.string 转 ushort
13.string 转 float
string 转 double
截取
14.获取指定长度字符串的值
15.清除SQL注入的危险字符
16.根据path递规创建文件夹
优点
1.一定编写随处可用。
2.调用代码简单,代码量少。
3.成熟代码无BUG
我是封装到DLL文件中,在具体的项目中引用。
引用命令空间
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Security;
代码
/// <summary>
/// 字符串扩展方法
/// </summary>
public static class StringExtension
{
#region 常量定义
//数字
private const string REGEX_NUMERIC = @"^[-]?\d+[.]?\d*$";
//邮件
private const string REGEX_EMAIL = @"^\w+([-+.]\w+)*@(\w+([-.]\w+)*\.)+([a-zA-Z]+)+$";
//中文字符
private const string REGEX_CHINESE_CHARACTER = @"^[\u4e00-\u9fa5]{0,}$";
#endregion
#region 判断
/// <summary>
/// 判断一个字符串是否为null或者空字符串
/// </summary>
/// <param name="str">要判断的字符串</param>
/// <returns>true: 空或者空字符串,false:不为空或者空字符串</returns>
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
/// <summary>
/// 判断一个字符串是否为null或者空白字符
/// </summary>
/// <param name="str">要判断的字符串</param>
/// <returns>true: 空或者空字符串,false:不为空或者空白字符</returns>
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
/// <summary>
/// 判断一个字符串是否为数字
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法数字,false: 输入字符串不是合法数字,空字符串返回false</returns>
public static bool IsNumeric(this string str)
{
if (str == null) return false;
if (string.IsNullOrEmpty(str))
{
return false;
}
str = str.Replace(",", String.Empty);
Regex regNum = new Regex(REGEX_NUMERIC);
return regNum.IsMatch(str);
}
/// <summary>
/// 判断一个字符串是否为邮件
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法邮件地址,false:输入字符串不是合法邮件地址,空字符串返回false</returns>
public static bool IsEmail(this string str)
{
if (str == null) return false;
Regex regex = new Regex(REGEX_EMAIL, RegexOptions.IgnoreCase);
return regex.Match(str).Success;
}
/// <summary>
/// 判断一个字符串是否为字母加数字
/// Regex("[a-zA-Z0-9]?"
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsWordAndNum(this string str)
{
Regex regex = new Regex("[a-zA-Z0-9]?");
return regex.Match(str).Success;
}
/// <summary>
/// 判断一个字符串是否为手机号码
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法的手机号码,false: 输入字符串不是合法的手机号码,空字符串返回false</returns>
public static bool IsMobileNum(this string str)
{
if (str == null) return false;
Regex regex = new Regex(@"^(13[0-9]|15[0-9]|18[0-9]|147|17[0-9])\d{8}$", RegexOptions.IgnoreCase);
return regex.Match(str).Success;
}
/// <summary>
/// 判断一个字符串是否为电话号码
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法的电话号码,false: 输入字符串不是合法的电话号码,空字符串返回false</returns>
public static bool IsPhoneNum(this string str)
{
if (str == null) return false;
Regex regex = new Regex(@"^(86)?(-)?(0\d{2,3})?(-)?(\d{7,8})(-)?(\d{3,5})?$", RegexOptions.IgnoreCase);
return regex.Match(str).Success;
}
/// <summary>
/// 判断一个字符串是否为网址
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法的网址,false: 输入字符串不是合法的网址,空字符串返回false</returns>
public static bool IsUrl(this string str)
{
if (str == null) return false;
Regex regex = new Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
return regex.Match(str).Success;
}
/// <summary>
/// 判断一个字符串是否为IP地址
/// </summary>
/// <param name="str">待检测字符串</param>
/// <returns>true: 输入字符串是合法的IP地址,false: 输入字符串不是合法的IP地址,空字符串返回false</returns>
public static bool IsIp(this string str)
{
if (str == null) return false;
Regex regex = new Regex(@"^(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1}))$", RegexOptions.IgnoreCase);
return regex.Match(str).Success;
}
/// <summary>
/// 判断是否为字母
/// </summary>
/// <param name="str">要校验的字符串</param>
/// <returns></returns>
public static bool IsLetters(this string str)
{
if (str == null) return false;
return Regex.IsMatch(str, @"^[a-zA-Z]");
}
/// <summary>
/// 判断密码是否符合规则
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsPassword(this string str, string pattern = "^[a-zA-Z0-9_]{6,18}$")
{
if (str == null) return false;
return Regex.IsMatch(str, @"" + pattern + "");
}
#endregion
#region 转换
/// <summary>
/// string 转 int
/// </summary>
/// <param name="value"></param>
/// <returns>转换失败返回int最小值,否则返回对应的值</returns>
public static int ToInt(this string value)
{
int val = int.MinValue;
int.TryParse(value, out val);
return val;
}
/// <summary>
/// string 转 int
/// </summary>
/// <param name="value"></param>
/// <param name="defaultVal">转换失败的默认值</param>
/// <returns>转换失败返回int最小值,否则返回对应的值</returns>
public static int ToInt(this string value,int defaultVal = int.MinValue)
{
int val = defaultVal;
int.TryParse(value, out val);
return val;
}
/// <summary>
/// string 转 ushort
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static ushort ToUshort(this string value)
{
return ushort.Parse(value.ToString());
}
/// <summary>
/// string 转 float
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static float ToFloat(this string value)
{
return float.Parse(value);
}
/// <summary>
/// string 转 double
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static double ToDouble(this string value)
{
double val = double.MinValue;
double.TryParse(value,out val);
return val;
}
#endregion
#region 截取
/// <summary>
/// 获取指定长度字符串的值
/// </summary>
/// <param name="str"></param>
/// <param name="length">指定长度</param>
/// <returns></returns>
public static string GetAssignLenVal(this string str, int length = 10)
{
string returnVal = string.Empty;
if (!string.IsNullOrEmpty(str))
{
returnVal = str.Length > length ? string.Format("{0}...", str.Substring(0, length)) : str;
}
return returnVal;
}
/// <summary>
/// 截取字符串
/// 如果转入的是null或''都返返回,string.Empty
/// </summary>
/// <param name="sourceStr">源字符串</param>
/// <param name="length">最大长度</param>
/// <returns>新字符串</returns>
public static string SubstringExtend(this string sourceStr, int length)
{
string resultStr = string.Empty;
if (!string.IsNullOrEmpty(sourceStr))
{
resultStr = sourceStr;
if (sourceStr.Length >= length)
{
resultStr = sourceStr.Substring(0, length - 1);
}
}
return resultStr;
}
#endregion
/// <summary>
/// 清除SQL注入的危险字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ClearDangerStr(this string str)
{
return str.ToLower().Replace("'", string.Empty).Replace(";", string.Empty).Replace("-", string.Empty).Replace("$", string.Empty).Replace(@"\", string.Empty).Replace(@"/", string.Empty)
.Replace("delete", string.Empty).Replace("update", string.Empty).Replace("drop", string.Empty).Replace("insert", string.Empty).Replace("select", string.Empty)
.Replace("alter", string.Empty).Replace("create", string.Empty);
}
/// <summary>
/// 根据path递规创建文件夹
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool CreateDirectoryByPath(this string filePath)
{
var isOk = false;
var filePathDirectory = filePath.Substring(0, filePath.LastIndexOf(@"\"));
filePathDirectory = filePathDirectory.Replace("//", @"\").Replace("/", @"\");
var directory = filePathDirectory.Split('\\').ToList();
var existsDir = string.Empty;
foreach (var item in directory)
{
existsDir += item + @"\";
if (!Directory.Exists(existsDir))
{
Directory.CreateDirectory(existsDir);
}
}
isOk = true;
return isOk;
}
}