using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace cylsg.utility.Extend { /// /// 字符串扩展 /// public static class StringEx { /// /// 首字母小写 /// /// /// public static string FirstToLower(this string self) { string str1 = self?.Substring(0, 1).ToLower() ?? ""; string str2 = self?.Substring(1) ?? ""; //首字母转小写 return str1 + str2; } /// /// 带有xxx.xxx结束的的所有字符串 的后面xxx.xxx /// /// /// public static string GetFileName(this string strtypr) { return Path.GetFileName(strtypr); } /// /// 是否是数字序列 /// /// /// public static bool isPureNum(this string strtypr) { if (strtypr.Length == 0 || strtypr == null)//验证这个字符串是否为空 { return false; } byte[] strBytes = Encoding.ASCII.GetBytes(strtypr);//获取字符串的byte类型的字符数组,编码方式ASCII foreach (byte strByte in strBytes) { if (strByte < 48 || strByte > 57) //判断每个字符是否为数字,根据每个字符的ASCII值所在范围判断 { return false; //不是,就返回false } } return true; //是,就返回true } /// /// 是否是数字序列 /// /// /// public static bool isGuid(this string strtypr) { if (strtypr.Length < 20 || strtypr == null)//验证这个字符串是否为空 { return false; } try { new Guid(strtypr); return true; } catch (Exception) { return false; } } /// /// 字符串 /// /// /// /// public static int toInt(this string strtypr) { if (!strtypr.isPureNum()) throw new Exception("并非完整的数字序列"); return int.Parse(strtypr); } /// /// 移除第一个字符串为指定字符串的 如 “axtXXXXX" 如果以"axt"开头的字符串,就移除"axt" 没有则不变 /// /// /// 开始字符 /// public static string RemoveStartWithStr(this string strtypr, string str) { string strret = strtypr; if (strtypr.StartsWith(str)) { strret = strtypr.Remove(0, str.Length); } return strret; } /// /// 对字符串进行隐私处理,比如中间的用*显示 /// /// /// public static string PrivacyStr(this string strtypr) { // 如果是手机号码,仅保留前3位和后4位,中间用星号替换 if (Regex.IsMatch(strtypr, @"^1[3-9]\d{9}$")) { return $"{strtypr.Substring(0, 3)}****{strtypr.Substring(7)}"; } // 对于中文名字或者非手机号格式,仅保留首尾各1个字符,中间用星号替换 else { if (strtypr.Length <= 2) return $"{strtypr.Substring(0, 1)}*"; // 长度小于等于2时,返回固定的星号 var masked = $"{strtypr.Substring(0, 1)}*{strtypr.Substring(strtypr.Length - 1, 1)}"; return masked; } } #region 时间区间字符串转其实和结束时间 /// /// 解析时间区间字符串 /// /// /// public static (DateTime? StartDate, DateTime? EndDate) TryParseDateRangString(this string input) { DateTime startDate, endDate; var culture = CultureInfo.InvariantCulture; // 尝试解析第一种格式 "2023-01-012023-01-31" if (DateTime.TryParseExact(input.Substring(0, 10), "yyyy-MM-dd", culture, DateTimeStyles.None, out startDate) && DateTime.TryParseExact(input.Substring(10), "yyyy-MM-dd", culture, DateTimeStyles.None, out endDate)) { return (startDate, endDate); } // 尝试解析第二种格式 "2023-01-01 08:02:032023-01-31 08:02:03" if (DateTime.TryParseExact(input.Substring(0, 19), "yyyy-MM-dd HH:mm:ss", culture, DateTimeStyles.None, out startDate) && DateTime.TryParseExact(input.Substring(19), "yyyy-MM-dd HH:mm:ss", culture, DateTimeStyles.None, out endDate)) { return (startDate, endDate); } // 尝试解析第二种格式 "2023-01-01T08:02:032023-01-31T08:02:03" if (DateTime.TryParseExact(input.Substring(0, 19), "yyyy-MM-ddTHH:mm:ss", culture, DateTimeStyles.None, out startDate) && DateTime.TryParseExact(input.Substring(19), "yyyy-MM-ddTHH:mm:ss", culture, DateTimeStyles.None, out endDate)) { return (startDate, endDate); } // 如果都不是,返回null return (null, null); } /// /// 移除最后一个字符的?号 /// /// /// /// 字符 /// public static string RemoveCharIfPresent(this string input, char inChar) { if (string.IsNullOrEmpty(input)) return input; // 检查字符串的最后一个字符是否为问号 if (input[^1] == '?') { // 使用切片操作符创建不包含问号的新字符串 return input[..^1]; } else { // 字符串不以问号结尾,返回原字符串 return input; } } /// /// 移除最后的字符串 /// /// /// /// public static string RemoveEndStringIfExists(this string input, string endString) { if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(endString)) { return input; // 如果输入字符串或结束字符串为空或null,直接返回原字符串 } if (input.EndsWith(endString)) { return input.Substring(0, input.Length - endString.Length); // 移除结束字符串 } return input; // 如果不以指定字符串结束,返回原字符串 } /// /// 首字母转小写 /// /// /// public static string FirstCharToLower(this string input) { if (string.IsNullOrEmpty(input)) { return input; } // 使用正则表达式 return Regex.Replace(input, @"^\p{Lu}", match => match.Value.ToLower()); } #endregion /// /// 首字母转大写 /// /// /// public static string FirstToCapitalize(this string input) { if (string.IsNullOrEmpty(input)) { return input; } TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; return textInfo.ToTitleCase(input.ToLower()).Substring(0, 1) + input.Substring(1); } } }