using System.Globalization; using System.Text; using System.Text.RegularExpressions; namespace XFEExtension.NetCore.StringExtension; /// /// 字符串拓展类 /// public static partial class StringExtension { #region 正则表达式 [GeneratedRegex(@"^(\d{3,4}-)?\d{6,8}$")] private static partial Regex TelePhoneRegex(); [GeneratedRegex(@"^1[3456789]\d{9}$")] private static partial Regex MobPhoneNumberRegex(); [GeneratedRegex(@"^\d{6}$")] private static partial Regex PostalCodeRegex(); [GeneratedRegex(@"^[0-9]*$")] private static partial Regex NumberRegex(); [GeneratedRegex(@"(^\d{18}$)|(^\d{15}$)")] private static partial Regex IdCardRegex(); [GeneratedRegex(@"(?:https?|www)\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z]{2,})+(?:\/[^\s]*)?")] private static partial Regex UrlRegex(); #endregion /// /// 判断字符串是否为座机号码 /// /// /// public static bool IsTelePhone(this string telephoneNum) { return TelePhoneRegex().IsMatch(telephoneNum); } /// /// 判断字符串是否为手机号码 /// /// /// public static bool IsMobPhoneNumber(this string mobPhoneNum) { return MobPhoneNumberRegex().IsMatch(mobPhoneNum); } /// /// 判断字符串是否为邮政编码 /// /// /// public static bool IsPostalCode(this string postalCode) { return PostalCodeRegex().IsMatch(postalCode); } /// /// 判断字符串是否为数字 /// /// /// public static bool IsNumber(this string number) { return NumberRegex().IsMatch(number); } /// /// 判断字符串是否为身份证件号 /// /// /// public static bool IsIdCard(this string idNumber) { return IdCardRegex().IsMatch(idNumber); } /// /// 判断字符串是否为 null 或者是空字符串 /// /// /// public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str); /// /// 判断字符串是否是 null 、空字符串和仅包含空格的字符串 /// /// /// public static bool IsNullOrWhiteSpace(this string str) => string.IsNullOrWhiteSpace(str); /// /// 判断字符串是否为邮箱地址 /// /// /// public static bool IsValidEmail(this string emailString) { if (string.IsNullOrWhiteSpace(emailString)) return false; try { emailString = Regex.Replace(emailString, @"(@)(.+)$", DomainMapper, RegexOptions.None, TimeSpan.FromMilliseconds(200)); var validDomain = new System.Net.Mail.MailAddress(emailString).Host; return Regex.IsMatch(emailString, @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)[0-9a-zA-Z]\@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,}))$", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)); } catch (FormatException) { return false; } } private static string DomainMapper(Match match) { var idn = new IdnMapping(); var domainName = match.Groups[2].Value; domainName = idn.GetAscii(domainName); return match.Groups[1].Value + domainName; } /// /// 验证是否是URL链接 /// /// 指定字符串 /// public static bool IsURL(this string str) { string pattern = @"^(https?|ftp|file|ws)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"; return IsMatch(pattern, str); } /// /// 判断一个字符串,是否匹配指定的表达式(区分大小写的情况下) /// /// 正则表达式 /// 要匹配的字符串 /// public static bool IsMatch(string expression, string str) { Regex reg = new(expression); if (string.IsNullOrEmpty(str)) return false; return reg.IsMatch(str); } /// /// 提取字符串中的URL链接 /// /// /// public static string[]? GetUrl(this string str) { if (str is not null) { Regex regex = UrlRegex(); MatchCollection matches = regex.Matches(str); string[] result = new string[matches.Count]; for (int i = 0; i < matches.Count; i++) { result[i] = matches[i].Value; } return result; } else { return null; } } /// /// 替换字符串中的多个字符串为指定字符串 /// /// /// 替换的字符串组 /// 目的字符串 /// public static string ReplaceStrings(this string str, string[] replaceStringArray, string tarString) { foreach (string replaceString in replaceStringArray) { str = str.Replace(replaceString, tarString); } return str; } /// /// 匹配两个字符串之间的字符串 /// /// /// 起始字符串 /// 结束字符串 /// public static string[] MatchStringsBetween(this string input, string startString, string endString) { string pattern = $"{Regex.Escape(startString)}(.*?){Regex.Escape(endString)}"; MatchCollection matches = Regex.Matches(input, pattern); string[] result = new string[matches.Count]; for (int i = 0; i < matches.Count; i++) { result[i] = matches[i].Groups[1].Value; } return result; } /// /// 文本自动换行 /// /// /// 一行文本的最大长度 /// public static string WrapText(this string text, int width) { StringBuilder stringBuilder = new(); int startIndex = 0; while (startIndex < text.Length) { // 寻找下一个换行位置 int endIndex = startIndex + width; if (endIndex >= text.Length) { // 如果已经到达文本末尾,则直接添加剩余部分并结束循环 stringBuilder.Append(text[startIndex..]); break; } else { // 在指定宽度范围内寻找最后一个空格字符 int lastSpaceIndex = text.LastIndexOf(' ', endIndex, width); if (lastSpaceIndex > startIndex) { // 如果找到了空格字符,则将文本从起始位置到空格字符位置添加到结果中,并进行换行 stringBuilder.Append(text[startIndex..lastSpaceIndex]); stringBuilder.AppendLine(); startIndex = lastSpaceIndex + 1; } else { // 如果没有找到空格字符,则直接添加指定宽度的文本到结果中,并进行换行 stringBuilder.Append(text.AsSpan(startIndex, width)); stringBuilder.AppendLine(); startIndex += width; } } } return stringBuilder.ToString(); } /// /// 输出到控制台并返回 /// /// /// ToString后的内容 public static string? WriteLineToConsole(this object obj) { Console.WriteLine(obj); return obj.ToString(); } /// /// 输出到控制台 /// /// public static void CW(this object obj) { Console.WriteLine(obj); } /// /// 字符串实际显示的长度 /// /// /// public static int DisplayLength(this string str) { int lengthCount = 0; var splits = str.ToCharArray(); for (int i = 0; i < splits.Length; i++) { if (splits[i] == '\t') { lengthCount += 8 - lengthCount % 8; } else { if (splits[i] > 255) { lengthCount += 2; } else { lengthCount += 1; } } } return lengthCount; } /// /// 随机生成指定长度的字符串 /// /// 字符串的长度 /// public static string GenerateRandomString(int length) { string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new(); char[] randomChars = new char[length]; for (int i = 0; i < length; i++) { int randomIndex = random.Next(0, allowedChars.Length); randomChars[i] = allowedChars[randomIndex]; } return new string(randomChars); } /// /// 随机生成指定长度指定字符串组合的字符串 /// /// 字符串的长度 /// 允许的字符串 /// public static string GenerateRandomString(int length, string allowedChars) { Random random = new(); char[] randomChars = new char[length]; for (int i = 0; i < length; i++) { int randomIndex = random.Next(0, allowedChars.Length); randomChars[i] = allowedChars[randomIndex]; } return new string(randomChars); } #region 返回包含分割器的字符串 /// /// 返回包含分割器的字符串 /// /// /// 分割器组 /// 是否移除空字符串 /// 分割后的带分割器字符串数组 public static string[] SplitAndKeepDelimiter(this string str, string[] delimiter, bool removeEmptyString) { string[] result; if (removeEmptyString) { result = str.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); } else { result = str.Split(delimiter, StringSplitOptions.None); } for (int i = 0; i < result.Length - 1; i++) { result[i] += delimiter; } return result; } /// /// 返回包含分割器的字符串 /// /// /// 分割器 /// 是否移除空字符串 /// 分割后的带分割器字符串数组 public static string[] SplitAndKeepDelimiter(this string str, string delimiter, bool removeEmptyString) { return SplitAndKeepDelimiter(str, new string[] { delimiter }, removeEmptyString); } /// /// 返回包含分割器的字符串 /// /// /// 分割器 /// 分割后的带分割器字符串数组 public static string[] SplitAndKeepDelimiter(this string str, string delimiter) { return SplitAndKeepDelimiter(str, new string[] { delimiter }, true); } /// /// 返回包含分割器的字符串 /// /// /// 分割器组 /// 分割后的带分割器字符串数组 public static string[] SplitAndKeepDelimiter(this string str, string[] delimiter) { return SplitAndKeepDelimiter(str, delimiter, true); } #endregion }