Files

261 lines
7.5 KiB
C#
Raw Permalink Normal View History

2026-02-20 16:47:26 +08:00
using System.Text;
using System.Text.RegularExpressions;
2026-02-21 16:31:24 +08:00
namespace ShrlAlgoToolkit.RevitAddins.Common.Extensions;
2026-02-20 16:47:26 +08:00
public static class StringExtensions
{
/// <summary>
/// 取得中文字符
/// </summary>
/// <param name="oriText"></param>
/// <returns></returns>
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();
}
/// <summary>
/// 固定间隔插入字符
/// </summary>
/// <param name="input">源字符串</param>
/// <param name="interval">间隔</param>
/// <param name="value">插入字符</param>
/// <returns></returns>
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;
}
/// <summary>
/// 判断更严谨,包含的有空串("")、空白符(空格""" ",制表符"\t",回车符"\r""\n"等)以及null值
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 是否为Double类型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static bool IsDouble(this string expression)
{
return expression != null && Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
}
/// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="expression">The expression<see cref="string" /></param>
/// <returns></returns>
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;
}
/// <summary>
/// 比较字符串相似度
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 获取最短长度
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
private static int Min(params int[] arr)
{
var min = int.MaxValue;
foreach (var i in arr)
{
if (min > i)
{
min = i;
}
}
return min;
}
/// <summary>
/// 数字转字母
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
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];
}
/// <summary>
/// 指定字符串的固定长度,如果字符串小于固定长度,
/// 则在字符串的前面补足零可设置的固定长度最大为9位
/// </summary>
/// <param name="text">原始字符串</param>
/// <param name="limitedLength">字符串的固定长度</param>
/// <returns>The <see cref="string" /></returns>
public static string RepairZero(this string text, int limitedLength)
{
return text.PadRight(limitedLength, '0');
}
/// <summary>
/// 使用指定字符集将string转换成byte[]
/// </summary>
/// <param name="text">要转换的字符串</param>
/// <param name="encoding">字符编码</param>
public static byte[] StringToBytes(this string text, Encoding encoding)
{
return encoding.GetBytes(text);
}
/// <summary>
/// string型转换为decimal型
/// </summary>
/// <param name="expression">The expression<see cref="string" /></param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的decimal类型结果</returns>
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;
}
/// <summary>
/// string型转换为float型
/// </summary>
/// <param name="expression">The expression<see cref="string" /></param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
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;
}
/// <summary>
/// 将字符串转换为Int32类型
/// </summary>
/// <param name="expression">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
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));
}
2026-03-01 10:42:42 +08:00
2026-02-20 16:47:26 +08:00
}