Sfoglia il codice sorgente

ref: 迁移 EnsureLength、IsAsciiOnly

龙腾猫跃 6 mesi fa
parent
commit
9f10b27c6b
1 ha cambiato i file con 13 aggiunte e 0 eliminazioni
  1. 13 0
      Extensions/StringExtensions.cs

+ 13 - 0
Extensions/StringExtensions.cs

@@ -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);
+
 }