#region copyright //------------------------------------------------------------------------------ // 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充 // 此代码版权(除特别声明外的代码)归作者本人Diego所有 // 源代码使用协议遵循本仓库的开源协议及附加协议 // Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway // Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway // 使用文档:https://diego2098.gitee.io/thingsgateway-docs/ // QQ群:605534569 //------------------------------------------------------------------------------ #endregion using Furion.DependencyInjection; using System.Text.RegularExpressions; namespace ThingsGateway.Admin.Core; /// /// 对象拓展 /// [SuppressSniffer] public static class StringExtensions { /// /// 返回List,无其他处理 /// public static List StringToList(this string str) { return new List() { str }; } /// /// 用 正则表达式 判断字符是不是汉字 /// /// 待判断字符或字符串 /// 真:是汉字;假:不是 private static bool IsChinese(string text) => Regex.IsMatch(text, @"[\u4e00-\u9fbb]"); /// /// 获取字符串中的两个字符作为名称简述 /// /// /// public static string GetNameLen2(this string name) { if (name.IsNullOrEmpty()) return string.Empty; var nameLength = name.Length;//获取姓名长度 string nameWritten = name;//需要绘制的文字 if (nameLength > 2)//如果名字长度超过2个 { // 如果用户输入的姓名大于等于3个字符,截取后面两位 string firstName = name.Substring(0, 1); if (IsChinese(firstName)) { // 截取倒数两位汉字 nameWritten = name.Substring(name.Length - 2); } else { // 截取第一个英文字母和第二个大写的字母 var data = Regex.Match(name, @"[A-Z]?[a-z]+([A-Z])").Value; nameWritten = data.FirstCharToUpper() + data.LastCharToUpper(); if (nameWritten.IsNullOrEmpty()) { nameWritten = name.FirstCharToUpper() + name.LastCharToUpper(); } } } return nameWritten; } /// /// 字符串是 null 或者 空 /// /// /// public static bool IsNullOrEmpty(this string value) => value == null || value!.Length <= 0; /// /// 返回字符串首字符的大写字母 /// /// /// public static string FirstCharToUpper(this string input) => input.IsNullOrEmpty() ? input : input.First().ToString().ToUpper(); /// /// 返回字符串尾字符的大写字母 /// /// /// public static string LastCharToUpper(this string input) => input.IsNullOrEmpty() ? input : input.Last().ToString().ToUpper(); /// /// 转换布尔值 /// /// public static bool ToBoolean(this string value, bool defaultValue = false) => value?.ToUpper() switch { "1" or "TRUE" => true, _ => defaultValue, }; /// /// ToLong /// /// public static long ToLong(this string value, long defaultValue = 0) => value.IsNullOrEmpty() ? defaultValue : Int64.TryParse(value, out var n) ? n : defaultValue; /// /// ToInt /// /// public static int ToInt(this string value, int defaultValue = 0) => value.IsNullOrEmpty() ? defaultValue : Int32.TryParse(value, out var n) ? n : defaultValue; /// /// ToDecimal /// /// public static decimal ToDecimal(this string value, int defaultValue = 0) => value.IsNullOrEmpty() ? defaultValue : Decimal.TryParse(value, out var n) ? n : defaultValue; /// /// 匹配手机号码 /// /// 源字符串 /// 是否匹配成功 public static bool MatchPhoneNumber(this string s) => !string.IsNullOrEmpty(s) && Regex.IsMatch(s, @"^1[3456789][0-9]{9}$"); /// /// 匹配邮箱格式 /// /// 源字符串 /// 是否匹配成功 public static bool MatchEmail(this string s) => !string.IsNullOrEmpty(s) && Regex.IsMatch(s, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); /// 合并多段路径 /// /// /// public static string CombinePath(this string path, params string[] ps) { if (ps == null || ps.Length <= 0) return path; path ??= string.Empty; foreach (var item in ps) { if (!item.IsNullOrEmpty()) path = Path.Combine(path, item); } return path; } }