diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs index dc842e0..dd70c87 100644 --- a/Extensions/StringExtensions.cs +++ b/Extensions/StringExtensions.cs @@ -168,4 +168,17 @@ public static class StringExtensions { public static string ReplaceOnDemand(this string str, string oldValue, Func newValue) => str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str; + /// + /// 将字符串统一至某个长度,过短则用 Code 将其左侧填充,过长则截取靠左的指定长度。 + /// + 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); + } + + /// + /// 该字符串中的字符是否均为 ASCII 字符。 + /// + public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => (int) c < 128); + }