using System.Text; using System.Text.RegularExpressions; namespace ShrlAlgoToolkit.RevitAddins.Common.Extensions; #region 格式化 //StringFormat={}{0:C} $123.46 //StringFormat={}{0:C1} $123.5 //StringFormat=单价:{0:C} 单价:$123.46 //StringFormat={}{0}元 123.45678元 //StringFormat={}{0:D6} 086723 //StringFormat={}{0:F4} 28768234.9329 //StringFormat={}{0:N3} 28,768,234.933 //StringFormat={}{0:P1} 78.9 % //StringFormat={}{0:0000.00} 0123.46 //StringFormat={}{0:####.##} 123.46 //StringFormat={}{0:d} 5/4/2015 //StringFormat={}{0:D} Monday, May 04, 2015 //StringFormat={}{0:f} Monday, May 04, 2015 5:46 PM //StringFormat={}{0:F} Monday, May 04, 2015 5:46:56 PM //StringFormat={}{0:g} 5/4/2015 5:46 PM //StringFormat={}{0:G} 5/4/2015 5:46:56 PM //StringFormat={}{0:m} May 04 //StringFormat={}{0:Distinct} May 04 //StringFormat={}{0:t} 5:46 PM //StringFormat={}{0:Command} 5:46:56 PM //StringFormat={}{0:yyyy年MM月dd日} 2015年05月04日 //StringFormat={}{0:yyyy-MM-dd} 2015-05-04 //StringFormat={}{0:yyyy-MM-dd HH:mm} 2015-05-04 17:46 //StringFormat={}{0:yyyy-MM-dd HH:mm:ss},,ConverterCulture=zh-CN||StringFormat='yyyy:MM:dd HH:mm:ss',,ConverterCulture=zh-CN 2015-05-04 17:46:56 //< TextBox.Text > // < MultiBinding StringFormat = "姓名:{0} {1}" > // < Binding Path = "FristName" /> // < Binding Path = "LastName" /> // // // < !-- // \a  BEL // \b  BS - Backspace // \f FF - Formfeed // \n LF, NL - Linefeed, New Line // \r CR - Carriage return // \t HT - Tab, Horizontal Tabelator // \v VT - Vertical Tabelator #endregion public static class StringExtensions { /// /// 取得中文字符 /// /// /// public static string GetChineseWord(this string oriText) { var x = @"[\u4E00-\u9FFF]+"; var matches = Regex.Matches(oriText, x, RegexOptions.IgnoreCase); var sb = new StringBuilder(); foreach (Match nextMatch in matches) { sb.Append(nextMatch.Value); } return sb.ToString(); } /// /// 固定间隔插入字符 /// /// 源字符串 /// 间隔 /// 插入字符 /// public static string InsertFormat(this string input, int interval, string value) { for (var i = interval - 1; i < input.Length; i += interval + 1) { input = input.Insert(i, value); } return input; } /// /// 判断更严谨,包含的有空串("")、空白符(空格""," ",制表符"\t",回车符"\r","\n"等)以及null值; /// /// /// public static bool IsBlank(this string str) { int strLen; if (str == null || (strLen = str.Length) == 0) { return true; } for (var i = 0; i < strLen; i++) { if (char.IsWhiteSpace(str.ElementAt(i)) == false) { return false; } } return true; } /// /// 是否为Double类型 /// /// /// public static bool IsDouble(this string expression) { return expression != null && Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); } /// /// 判断对象是否为Int32类型的数字 /// /// The expression /// public static bool IsNumeric(this string expression) { if (expression != null) { var str = expression; if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) { if (str.Length < 10 || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) { return true; } } } return false; } /// /// 比较字符串相似度 /// /// /// /// public static float Levenshtein(this string str1, string str2) { var len1 = str1.Length; var len2 = str2.Length; var dif = new int[len1 + 1, len2 + 1]; for (var a = 0; a <= len1; a++) { dif[a, 0] = a; } for (var a = 0; a <= len2; a++) { dif[0, a] = a; } int temp; for (var i = 1; i <= len1; i++) { for (var j = 1; j <= len2; j++) { temp = str1.ElementAt(i - 1) == str2.ElementAt(j - 1) ? 0 : 1; dif[i, j] = Min(dif[i - 1, j - 1] + temp, dif[i, j - 1] + 1, dif[i - 1, j] + 1); } } var similarity = 1 - ((float)dif[len1, len2] / Math.Max(len1, len2)); return similarity; } /// /// 获取最短长度 /// /// /// private static int Min(params int[] arr) { var min = int.MaxValue; foreach (var i in arr) { if (min > i) { min = i; } } return min; } /// /// 数字转字母 /// /// /// public static string NumberToLetter(int index) { var str = string.Empty; if (index >= "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Length) { var num = index / "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Length; index %= "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Length; str += num.ToString(); } return str + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index]; } /// /// 指定字符串的固定长度,如果字符串小于固定长度, /// 则在字符串的前面补足零,可设置的固定长度最大为9位 /// /// 原始字符串 /// 字符串的固定长度 /// The public static string RepairZero(this string text, int limitedLength) { return text.PadRight(limitedLength, '0'); } /// /// 使用指定字符集将string转换成byte[] /// /// 要转换的字符串 /// 字符编码 public static byte[] StringToBytes(this string text, Encoding encoding) { return encoding.GetBytes(text); } /// /// string型转换为decimal型 /// /// The expression /// 缺省值 /// 转换后的decimal类型结果 public static decimal StrToDecimal(this string expression, decimal defValue) { if (expression == null || expression.Length > 10) { return defValue; } var intValue = defValue; { var isDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (isDecimal) { decimal.TryParse(expression, out intValue); } } return intValue; } /// /// string型转换为float型 /// /// The expression /// 缺省值 /// 转换后的int类型结果 public static float StrToFloat(this string expression, float defValue) { if (expression == null || expression.Length > 10) { return defValue; } var intValue = defValue; { var isFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (isFloat) { float.TryParse(expression, out intValue); } } return intValue; } /// /// 将字符串转换为Int32类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static int StrToInt(this string expression, int defValue) { return string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$") ? defValue : int.TryParse(expression, out var rv) ? rv : Convert.ToInt32(StrToFloat(expression, defValue)); } }