using System.Text; using System.Text.RegularExpressions; namespace JNPF.Extras.CollectiveOAuth.Utils; /// /// 字符串类型的扩展辅助操作类. /// public static class StringExtensions { #region 正则表达式 /// /// 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项. /// /// 要搜索匹配项的字符串. /// 要匹配的正则表达式模式. /// 如果正则表达式找到匹配项,则为 true;否则,为 false. public static bool IsMatch(this string value, string pattern) { if (value == null) { return false; } return Regex.IsMatch(value, pattern); } /// /// 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项. /// /// 要搜索匹配项的字符串. /// 要匹配的正则表达式模式. /// 一个对象,包含有关匹配项的信息. public static string Match(this string value, string pattern) { if (value == null) return null; return Regex.Match(value, pattern).Value; } /// /// 在指定的输入字符串中搜索指定的正则表达式的所有匹配项的字符串集合. /// /// 要搜索匹配项的字符串. /// 要匹配的正则表达式模式. /// 一个集合,包含有关匹配项的字符串值. public static IEnumerable Matches(this string value, string pattern) { if (value == null) return new string[] { }; MatchCollection matches = Regex.Matches(value, pattern); return from Match match in matches select match.Value; } /// /// 是否电子邮件. /// public static bool IsEmail(this string value) { const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return value.IsMatch(pattern); } /// /// 是否是IP地址. /// public static bool IsIpAddress(this string value) { const string pattern = @"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$"; return value.IsMatch(pattern); } /// /// 是否是整数. /// public static bool IsNumeric(this string value) { const string pattern = @"^\-?[0-9]+$"; return value.IsMatch(pattern); } /// /// 是否是Unicode字符串. /// public static bool IsUnicode(this string value) { const string pattern = @"^[\u4E00-\u9FA5\uE815-\uFA29]+$"; return value.IsMatch(pattern); } /// /// 是否Url字符串. /// public static bool IsUrl(this string value) { const string pattern = @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"; return value.IsMatch(pattern); } /// /// 是否身份证号,验证如下3种情况: /// 1.身份证号码为15位数字; /// 2.身份证号码为18位数字; /// 3.身份证号码为17位数字+1个字母. /// public static bool IsIdentityCard(this string value) { const string pattern = @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"; return value.IsMatch(pattern); } /// /// 是否手机号码. /// /// /// 是否按严格格式验证. public static bool IsMobileNumber(this string value, bool isRestrict = false) { string pattern = isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$"; return value.IsMatch(pattern); } #endregion #region 其他操作 /// /// 指示指定的字符串是 null 还是 System.String.Empty 字符串. /// public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } /// /// 指示指定的字符串是 null、空还是仅由空白字符组成. /// public static bool IsNullOrWhiteSpace(this string value) { return string.IsNullOrWhiteSpace(value); } /// /// 判断指定路径是否图片文件. /// public static bool IsImageFile(this string filename) { if (!File.Exists(filename)) { return false; } byte[] filedata = File.ReadAllBytes(filename); if (filedata.Length == 0) return false; switch (BitConverter.ToUInt16(filedata, 0)) { case 0x4D42: // bmp case 0xD8FF: // jpg case 0x4947: // gif case 0x5089: // png return true; default: return false; } } /// /// 以指定字符串作为分隔符将指定字符串分隔成数组. /// /// 要分割的字符串. /// 字符串类型的分隔符. /// 是否移除数据中元素为空字符串的项. /// 分割后的数据 public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false) { return value.Split(new[] { strSplit }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None); } /// /// 支持汉字的字符串长度,汉字长度计为2. /// /// 参数字符串. /// 当前字符串的长度,汉字长度为2. public static int TextLength(this string value) { ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; byte[] bytes = ascii.GetBytes(value); foreach (byte b in bytes) { if (b == 63) tempLen += 2; else tempLen += 1; } return tempLen; } /// /// 将字符串转换为[]数组,默认编码为. /// public static byte[] ToBytes(this string value, Encoding encoding = null) { if (encoding == null) encoding = Encoding.UTF8; return encoding.GetBytes(value); } /// /// 将[]数组转换为字符串,默认编码为. /// public static string ToString(this byte[] bytes, Encoding encoding) { if (encoding == null) encoding = Encoding.UTF8; return encoding.GetString(bytes); } #endregion }