ref: 迁移 EnsureLength、IsAsciiOnly

This commit is contained in:
龙腾猫跃 committed 2026-04-27 18:07:34 +08:00
1 parent b8a35c78ff
commit 9f10b27c6b
1 file changed
+13
+13
View File
@@ -168,4 +168,17 @@ public static class StringExtensions {
public static string ReplaceOnDemand(this string str, string oldValue, Func<string> newValue) =>
str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str;
/// <summary>
/// 将字符串统一至某个长度,过短则用 Code 将其左侧填充,过长则截取靠左的指定长度。
/// </summary>
public static string EnsureLength(this string? str, char code, int length) {
if (str == null) str = "";
return str.Length > length ? str.Substring(0, length) : str.PadLeft(length, code);
}
/// <summary>
/// 该字符串中的字符是否均为 ASCII 字符。
/// </summary>
public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => (int) c < 128);
}