using Microsoft.VisualBasic; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace EC.Utils { /// /// 字符串操作 - 工具方法 /// public sealed partial class Str { #region Empty(空字符串) /// /// 空字符串 /// public static string Empty { get { return string.Empty; } } #endregion Empty(空字符串) #region PinYin(获取汉字的拼音简码) /// /// 使用字符编码方式获取拼音简码 /// private static string ResolvePinYinByCode(ushort unicode) { if (unicode >= '\uB0A1' && unicode <= '\uB0C4') return "A"; if (unicode >= '\uB0C5' && unicode <= '\uB2C0' && unicode != 45464) return "B"; if (unicode >= '\uB2C1' && unicode <= '\uB4ED') return "C"; if (unicode >= '\uB4EE' && unicode <= '\uB6E9') return "D"; if (unicode >= '\uB6EA' && unicode <= '\uB7A1') return "E"; if (unicode >= '\uB7A2' && unicode <= '\uB8C0') return "F"; if (unicode >= '\uB8C1' && unicode <= '\uB9FD') return "G"; if (unicode >= '\uB9FE' && unicode <= '\uBBF6') return "H"; if (unicode >= '\uBBF7' && unicode <= '\uBFA5') return "J"; if (unicode >= '\uBFA6' && unicode <= '\uC0AB') return "K"; if (unicode >= '\uC0AC' && unicode <= '\uC2E7') return "L"; if (unicode >= '\uC2E8' && unicode <= '\uC4C2') return "M"; if (unicode >= '\uC4C3' && unicode <= '\uC5B5') return "N"; if (unicode >= '\uC5B6' && unicode <= '\uC5BD') return "O"; if (unicode >= '\uC5BE' && unicode <= '\uC6D9') return "P"; if (unicode >= '\uC6DA' && unicode <= '\uC8BA') return "Q"; if (unicode >= '\uC8BB' && unicode <= '\uC8F5') return "R"; if (unicode >= '\uC8F6' && unicode <= '\uCBF9') return "S"; if (unicode >= '\uCBFA' && unicode <= '\uCDD9') return "T"; if (unicode >= '\uCDDA' && unicode <= '\uCEF3') return "W"; if (unicode >= '\uCEF4' && unicode <= '\uD188') return "X"; if (unicode >= '\uD1B9' && unicode <= '\uD4D0') return "Y"; if (unicode >= '\uD4D1' && unicode <= '\uD7F9') return "Z"; return string.Empty; } #endregion PinYin(获取汉字的拼音简码) #region Splice(拼接集合元素) /// /// 拼接集合元素 /// /// 集合元素类型 /// 集合 /// 引号,默认不带引号,范例:单引号 "'" /// 分隔符,默认使用逗号分隔 public static string Splice(IEnumerable list, string quotes = "", string separator = ",") { if (list == null) return string.Empty; var result = new StringBuilder(); foreach (var each in list) result.AppendFormat("{0}{1}{0}{2}", quotes, each, separator); return result.ToString().TrimEnd(separator.ToCharArray()); } #endregion Splice(拼接集合元素) #region FirstUpper(将值的首字母大写) /// /// 将值的首字母大写 /// /// 值 public static string FirstUpper(string value) { string firstChar = value.Substring(0, 1).ToUpper(); return firstChar + value.Substring(1, value.Length - 1); } #endregion FirstUpper(将值的首字母大写) #region ToCamel(将字符串转成驼峰形式) /// /// 将字符串转成驼峰形式 /// /// 原始字符串 public static string ToCamel(string value) { return FirstUpper(value.ToLower()); } #endregion ToCamel(将字符串转成驼峰形式) #region ContainsChinese(是否包含中文) /// /// 是否包含中文 /// /// 文本 public static bool ContainsChinese(string text) { const string pattern = "[\u4e00-\u9fa5]+"; return Regex.IsMatch(text, pattern); } #endregion ContainsChinese(是否包含中文) #region ContainsNumber(是否包含数字) /// /// 是否包含数字 /// /// 文本 public static bool ContainsNumber(string text) { const string pattern = "[0-9]+"; return Regex.IsMatch(text, pattern); } #endregion ContainsNumber(是否包含数字) #region Distinct(去除重复) /// /// 去除重复 /// /// 值,范例1:"5555",返回"5",范例2:"4545",返回"45" public static string Distinct(string value) { var array = value.ToCharArray(); return new string(array.Distinct().ToArray()); } #endregion Distinct(去除重复) #region Truncate(截断字符串) /// /// 截断字符串 /// /// 文本 /// 返回长度 /// 添加结束符号的个数,默认0,不添加 /// 结束符号,默认为省略号 public static string Truncate(string text, int length, int endCharCount = 0, string endChar = ".") { if (string.IsNullOrWhiteSpace(text)) return string.Empty; if (text.Length < length) return text; return text.Substring(0, length) + GetEndString(endCharCount, endChar); } /// /// 获取结束字符串 /// private static string GetEndString(int endCharCount, string endChar) { StringBuilder result = new StringBuilder(); for (int i = 0; i < endCharCount; i++) result.Append(endChar); return result.ToString(); } #endregion Truncate(截断字符串) #region ToSimplifiedChinese(转换为简体中文) /// /// 转换为简体中文 /// /// 繁体中文 public static string ToSimplifiedChinese(string text) { return Strings.StrConv(text, VbStrConv.SimplifiedChinese); } #endregion ToSimplifiedChinese(转换为简体中文) #region ToSimplifiedChinese(转换为繁体中文) /// /// 转换为繁体中文 /// /// 简体中文 public static string ToTraditionalChinese(string text) { return Strings.StrConv(text, VbStrConv.TraditionalChinese); } #endregion ToSimplifiedChinese(转换为繁体中文) #region Unique(获取全局唯一值) /// /// 获取全局唯一值 /// public static string Unique() { return Guid.NewGuid().ToString().Replace("-", ""); } #endregion Unique(获取全局唯一值) #region GetLastProperty(获取最后一个属性) /// /// 获取最后一个属性 /// /// 属性名,范例,A.B.C,返回"C" public static string GetLastProperty(string propertyName) { if (string.IsNullOrEmpty(propertyName)) return string.Empty; var lastIndex = propertyName.LastIndexOf(".", StringComparison.Ordinal) + 1; return propertyName.Substring(lastIndex); } #endregion GetLastProperty(获取最后一个属性) } }