Class OpenString

java.lang.Object
cloud.opencode.base.string.OpenString

public final class OpenString extends Object
Enhanced String Utility Facade 字符串增强工具门面类

Provides comprehensive string operations including padding, truncation, transformation, etc.

提供全面的字符串操作,包括填充、截断、转换等。

Features | 主要功能:

  • Padding and truncation - 填充和截断
  • Case conversion - 大小写转换
  • String manipulation - 字符串操作
  • Substring extraction - 子串提取
  • Validation and checking - 验证和检查

Usage Examples | 使用示例:

// Padding
String padded = OpenString.padLeft("42", 5, '0'); // "00042"

// Truncation
String truncated = OpenString.truncate("Hello World", 8); // "Hello..."

// Substring extraction
String between = OpenString.substringBetween("[hello]", "[", "]"); // "hello"

// Statistics
int count = OpenString.countMatches("banana", "an"); // 2

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-string V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • padLeft

      public static String padLeft(String str, int minLength, char padChar)
      Pad string on the left to reach minimum length. 在字符串左侧填充以达到最小长度。
      Parameters:
      str - the input string, may be null (returns null if so)
      minLength - the minimum desired length; must not be negative
      padChar - the character to pad with
      Returns:
      the padded string, or null if input was null
      Throws:
      IllegalArgumentException - if minLength is negative
    • padRight

      public static String padRight(String str, int minLength, char padChar)
      Pad string on the right to reach minimum length. 在字符串右侧填充以达到最小长度。
      Parameters:
      str - the input string, may be null (returns null if so)
      minLength - the minimum desired length; must not be negative
      padChar - the character to pad with
      Returns:
      the padded string, or null if input was null
      Throws:
      IllegalArgumentException - if minLength is negative
    • center

      public static String center(String str, int minLength, char padChar)
      Center string within the given minimum length using the pad character. 使用填充字符将字符串居中至给定的最小长度。
      Parameters:
      str - the input string, may be null (returns null if so)
      minLength - the minimum desired length; must not be negative
      padChar - the character to pad with
      Returns:
      the centered string, or null if input was null
      Throws:
      IllegalArgumentException - if minLength is negative
    • left

      public static String left(String str, int length)
      Get the leftmost length characters of a string. 获取字符串最左侧的 length 个字符。
      Parameters:
      str - the input string, may be null (returns null if so)
      length - the number of characters to extract; negative values return null input unchanged
      Returns:
      the leftmost characters, or null if input was null
    • right

      public static String right(String str, int length)
      Get the rightmost length characters of a string. 获取字符串最右侧的 length 个字符。
      Parameters:
      str - the input string, may be null (returns null if so)
      length - the number of characters to extract; negative values return null input unchanged
      Returns:
      the rightmost characters, or null if input was null
    • mid

      public static String mid(String str, int start, int end)
    • truncate

      public static String truncate(String str, int maxLength)
    • truncate

      public static String truncate(String str, int maxLength, String ellipsis)
    • truncateMiddle

      public static String truncateMiddle(String str, int maxLength)
    • truncateByBytes

      public static String truncateByBytes(String str, int maxBytes, String charset)
    • capitalize

      public static String capitalize(String str)
    • uncapitalize

      public static String uncapitalize(String str)
    • swapCase

      public static String swapCase(String str)
    • toTitleCase

      public static String toTitleCase(String str)
    • reverse

      public static String reverse(String str)
    • shuffle

      public static String shuffle(String str)
    • shuffle

      public static String shuffle(String str, Random random)
    • countMatches

      public static int countMatches(String str, String subStr)
    • countMatches

      public static int countMatches(String str, char ch)
    • charFrequency

      public static Map<Character, Integer> charFrequency(String str)
    • wordFrequency

      public static Map<String,Integer> wordFrequency(String str)
    • removeWhitespace

      public static String removeWhitespace(String str)
    • normalizeWhitespace

      public static String normalizeWhitespace(String str)
    • removeInvisibleChars

      public static String removeInvisibleChars(String str)
    • removeSpecialChars

      public static String removeSpecialChars(String str)
    • keepDigits

      public static String keepDigits(String str)
    • keepLetters

      public static String keepLetters(String str)
    • keepAlphanumeric

      public static String keepAlphanumeric(String str)
    • keepChinese

      public static String keepChinese(String str)
    • isNumeric

      public static boolean isNumeric(String str)
    • isAlpha

      public static boolean isAlpha(String str)
    • isAlphanumeric

      public static boolean isAlphanumeric(String str)
    • isAscii

      public static boolean isAscii(String str)
    • containsChinese

      public static boolean containsChinese(String str)
    • isAllChinese

      public static boolean isAllChinese(String str)
    • isAllLowerCase

      public static boolean isAllLowerCase(String str)
    • isAllUpperCase

      public static boolean isAllUpperCase(String str)
    • isMixedCase

      public static boolean isMixedCase(String str)
    • isPalindrome

      public static boolean isPalindrome(String str)
    • isPalindromeIgnoreCase

      public static boolean isPalindromeIgnoreCase(String str)
    • ensurePrefix

      public static String ensurePrefix(String str, String prefix)
    • ensureSuffix

      public static String ensureSuffix(String str, String suffix)
    • ensureWrap

      public static String ensureWrap(String str, String wrap)
    • removePrefix

      public static String removePrefix(String str, String prefix)
    • removeSuffix

      public static String removeSuffix(String str, String suffix)
    • commonPrefix

      public static String commonPrefix(String... strs)
    • commonSuffix

      public static String commonSuffix(String... strs)
    • findAll

      public static List<Integer> findAll(String str, String subStr)
    • indexOfNth

      public static int indexOfNth(String str, String subStr, int n)
    • lastIndexOfNth

      public static int lastIndexOfNth(String str, String subStr, int n)
    • substringBefore

      public static String substringBefore(String str, String separator)
    • substringAfter

      public static String substringAfter(String str, String separator)
    • substringBeforeLast

      public static String substringBeforeLast(String str, String separator)
    • substringAfterLast

      public static String substringAfterLast(String str, String separator)
    • substringBetween

      public static String substringBetween(String str, String open, String close)
    • substringsBetween

      public static String[] substringsBetween(String str, String open, String close)
    • wrap

      public static String wrap(String str, char wrapChar)
    • wrap

      public static String wrap(String str, String wrapStr)
    • unwrap

      public static String unwrap(String str, char wrapChar)
    • unwrap

      public static String unwrap(String str, String wrapStr)
    • chomp

      public static String chomp(String str)
    • chop

      public static String chop(String str)
    • stripAccents

      public static String stripAccents(String str)
    • getDigits

      public static String getDigits(String str)
    • rotate

      public static String rotate(String str, int shift)
    • difference

      public static String difference(String str1, String str2)
    • indexOfDifference

      public static int indexOfDifference(String str1, String str2)
    • abbreviateMiddle

      public static String abbreviateMiddle(String str, String middle, int maxLength)
    • isRepeated

      public static boolean isRepeated(String str)
    • getRepeatedPattern

      public static String getRepeatedPattern(String str)
    • defaultIfBlank

      public static String defaultIfBlank(String str, String defaultStr)
    • defaultIfEmpty

      public static String defaultIfEmpty(String str, String defaultStr)
    • firstNonBlank

      public static String firstNonBlank(String... strs)
    • firstNonEmpty

      public static String firstNonEmpty(String... strs)
    • isBlank

      public static boolean isBlank(String str)
      Check if a string is blank (null, empty, or whitespace only). 检查字符串是否为空白(null、空字符串或仅包含空白字符)。

      Examples | 示例:

      isBlank(null)   = true
      isBlank("")     = true
      isBlank("  ")   = true
      isBlank("abc")  = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      Returns:
      true if the string is null, empty, or whitespace only | 如果字符串为null、空或仅含空白则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • isNotBlank

      public static boolean isNotBlank(String str)
      Check if a string is not blank. 检查字符串是否不为空白。

      Examples | 示例:

      isNotBlank(null)   = false
      isNotBlank("")     = false
      isNotBlank("  ")   = false
      isNotBlank("abc")  = true
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      Returns:
      true if the string is not null, not empty, and not whitespace only | 如果字符串非null、非空且非纯空白则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • isEmpty

      public static boolean isEmpty(String str)
      Check if a string is empty (null or zero length). 检查字符串是否为空(null或长度为零)。

      Examples | 示例:

      isEmpty(null)   = true
      isEmpty("")     = true
      isEmpty("  ")   = false
      isEmpty("abc")  = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      Returns:
      true if the string is null or empty | 如果字符串为null或空则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • isNotEmpty

      public static boolean isNotEmpty(String str)
      Check if a string is not empty. 检查字符串是否不为空。

      Examples | 示例:

      isNotEmpty(null)   = false
      isNotEmpty("")     = false
      isNotEmpty("  ")   = true
      isNotEmpty("abc")  = true
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      Returns:
      true if the string is not null and not empty | 如果字符串非null且非空则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • containsAny

      public static boolean containsAny(String str, CharSequence... searchStrings)
      Check if a string contains any of the given search strings. 检查字符串是否包含给定搜索字符串中的任意一个。

      Examples | 示例:

      containsAny("hello world", "world", "foo") = true
      containsAny("hello", "foo", "bar")         = false
      containsAny(null, "foo")                    = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      searchStrings - the strings to search for | 要搜索的字符串
      Returns:
      true if the string contains any of the search strings | 如果字符串包含任一搜索字符串则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • containsNone

      public static boolean containsNone(String str, String invalidChars)
      Check if a string contains none of the given invalid characters. 检查字符串是否不包含任何给定的无效字符。

      Examples | 示例:

      containsNone("hello", "xyz")  = true
      containsNone("hello", "hxyz") = false
      containsNone(null, "xyz")     = true
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      invalidChars - characters that should not appear | 不应出现的字符
      Returns:
      true if the string contains none of the invalid characters | 如果字符串不包含任何无效字符则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • containsOnly

      public static boolean containsOnly(String str, String validChars)
      Check if a string contains only the given valid characters. 检查字符串是否仅包含给定的有效字符。

      Examples | 示例:

      containsOnly("aab", "abc")  = true
      containsOnly("abd", "abc")  = false
      containsOnly(null, "abc")   = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      validChars - characters that are allowed | 允许的字符
      Returns:
      true if the string contains only valid characters | 如果字符串仅包含有效字符则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • containsIgnoreCase

      public static boolean containsIgnoreCase(String str, String search)
      Check if a string contains another string, ignoring case. 忽略大小写检查字符串是否包含另一个字符串。

      Examples | 示例:

      containsIgnoreCase("Hello World", "hello") = true
      containsIgnoreCase("Hello World", "foo")   = false
      containsIgnoreCase(null, "hello")           = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      search - the string to search for, may be null | 要搜索的字符串,可为 null
      Returns:
      true if the string contains the search string ignoring case | 如果忽略大小写后字符串包含搜索字符串则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • startsWithAny

      public static boolean startsWithAny(String str, String... prefixes)
      Check if a string starts with any of the given prefixes. 检查字符串是否以给定前缀中的任意一个开头。

      Examples | 示例:

      startsWithAny("hello", "he", "ho") = true
      startsWithAny("hello", "foo")      = false
      startsWithAny(null, "he")          = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      prefixes - the prefixes to check | 要检查的前缀
      Returns:
      true if the string starts with any of the prefixes | 如果字符串以任一前缀开头则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • endsWithAny

      public static boolean endsWithAny(String str, String... suffixes)
      Check if a string ends with any of the given suffixes. 检查字符串是否以给定后缀中的任意一个结尾。

      Examples | 示例:

      endsWithAny("hello", "lo", "la") = true
      endsWithAny("hello", "foo")      = false
      endsWithAny(null, "lo")          = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      suffixes - the suffixes to check | 要检查的后缀
      Returns:
      true if the string ends with any of the suffixes | 如果字符串以任一后缀结尾则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String str, String prefix)
      Check if a string starts with a prefix, ignoring case. 忽略大小写检查字符串是否以指定前缀开头。

      Examples | 示例:

      startsWithIgnoreCase("Hello", "he")  = true
      startsWithIgnoreCase("Hello", "HE")  = true
      startsWithIgnoreCase("Hello", "foo") = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      prefix - the prefix to check, may be null | 要检查的前缀,可为 null
      Returns:
      true if the string starts with the prefix ignoring case | 如果忽略大小写后字符串以该前缀开头则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String str, String suffix)
      Check if a string ends with a suffix, ignoring case. 忽略大小写检查字符串是否以指定后缀结尾。

      Examples | 示例:

      endsWithIgnoreCase("Hello", "LO")  = true
      endsWithIgnoreCase("Hello", "lo")  = true
      endsWithIgnoreCase("Hello", "foo") = false
      
      Parameters:
      str - the string to check, may be null | 待检查的字符串,可为 null
      suffix - the suffix to check, may be null | 要检查的后缀,可为 null
      Returns:
      true if the string ends with the suffix ignoring case | 如果忽略大小写后字符串以该后缀结尾则返回true
      Since:
      JDK 25, opencode-base-string V1.0.3
    • replaceEach

      public static String replaceEach(String text, String[] searchList, String[] replacementList)
      Replace all occurrences of search strings with corresponding replacement strings in a single pass. 单趟扫描替换所有搜索字符串为对应的替换字符串,不递归替换。

      This method scans the input text once and replaces each occurrence of a search string with the corresponding replacement. Replacements are not applied recursively.

      本方法对输入文本进行单趟扫描,将每个搜索字符串替换为对应的替换字符串。替换不会递归应用。

      Examples | 示例:

      replaceEach("aabbcc", new String[]{"aa","bb"}, new String[]{"11","22"}) = "1122cc"
      replaceEach("abcde", new String[]{"ab","d"}, new String[]{"w","t"})     = "wcte"
      
      Parameters:
      text - the text to search and replace in, may be null | 要搜索替换的文本,可为 null
      searchList - the strings to search for, may be null | 要搜索的字符串数组,可为 null
      replacementList - the strings to replace them with, may be null | 替换字符串数组,可为 null
      Returns:
      the text with replacements applied, or original text if no replacements needed | 替换后的文本
      Throws:
      IllegalArgumentException - if searchList and replacementList have different lengths | 如果搜索和替换数组长度不同
      Since:
      JDK 25, opencode-base-string V1.0.3
    • format

      public static String format(String pattern, Object... args)
      Format a string using SLF4J-style {} placeholders. 使用SLF4J风格的 {} 占位符格式化字符串。

      Placeholders are replaced left-to-right with the provided arguments. Use \{} to escape a placeholder (outputs literal {}). Excess arguments are ignored; insufficient arguments leave placeholders intact.

      占位符从左到右依次替换为提供的参数。 使用 \{} 转义占位符(输出字面量 {})。 多余参数被忽略;参数不足时保留未替换的占位符。

      Examples | 示例:

      format("{} has {} items", "Alice", 3) = "Alice has 3 items"
      format("{} and {}", "A")              = "A and {}"
      format("literal \\{}", "ignored")      = "literal {}"
      
      Parameters:
      pattern - the pattern string with {} placeholders, may be null | 包含占位符的模式字符串,可为 null
      args - the arguments to fill in placeholders | 用于填充占位符的参数
      Returns:
      the formatted string, or null if pattern is null | 格式化后的字符串,pattern为null时返回null
      Since:
      JDK 25, opencode-base-string V1.0.3
    • nullToEmpty

      public static String nullToEmpty(String str)
      Convert a null string to an empty string. 将 null 字符串转换为空字符串。

      Examples | 示例:

      nullToEmpty(null)    = ""
      nullToEmpty("")      = ""
      nullToEmpty("hello") = "hello"
      
      Parameters:
      str - the string, may be null | 字符串,可为 null
      Returns:
      the original string or empty string if null | 原字符串或null时返回空字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • emptyToNull

      public static String emptyToNull(String str)
      Convert an empty string to null. 将空字符串转换为 null

      Examples | 示例:

      emptyToNull("")      = null
      emptyToNull(null)    = null
      emptyToNull("hello") = "hello"
      emptyToNull("  ")    = "  "
      
      Parameters:
      str - the string, may be null | 字符串,可为 null
      Returns:
      null if the string is empty, otherwise the original string | 字符串为空时返回null,否则返回原字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • blankToNull

      public static String blankToNull(String str)
      Convert a blank string (null, empty, or whitespace only) to null. 将空白字符串(null、空字符串或仅含空白字符)转换为 null

      Examples | 示例:

      blankToNull("")      = null
      blankToNull("  ")    = null
      blankToNull(null)    = null
      blankToNull("hello") = "hello"
      
      Parameters:
      str - the string, may be null | 字符串,可为 null
      Returns:
      null if the string is blank, otherwise the original string | 字符串为空白时返回null,否则返回原字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • repeat

      public static String repeat(String str, String separator, int count)
      Repeat a string with a separator between each occurrence. 使用分隔符重复字符串。

      Examples | 示例:

      repeat("abc", ", ", 3) = "abc, abc, abc"
      repeat("x", "-", 1)   = "x"
      repeat("x", "-", 0)   = ""
      repeat(null, "-", 3)  = null
      
      Parameters:
      str - the string to repeat, may be null | 要重复的字符串,可为 null
      separator - the separator between repetitions | 重复之间的分隔符
      count - the number of repetitions; <= 0 returns empty string | 重复次数;小于等于0返回空字符串
      Returns:
      the repeated string with separators, or null if str is null | 带分隔符的重复字符串,str为null时返回null
      Since:
      JDK 25, opencode-base-string V1.0.3
    • removePrefixIgnoreCase

      public static String removePrefixIgnoreCase(String str, String prefix)
      Remove a prefix from a string, ignoring case. 忽略大小写移除字符串的前缀。

      Examples | 示例:

      removePrefixIgnoreCase("HelloWorld", "hello") = "World"
      removePrefixIgnoreCase("HelloWorld", "HELLO") = "World"
      removePrefixIgnoreCase("HelloWorld", "foo")   = "HelloWorld"
      
      Parameters:
      str - the string, may be null | 字符串,可为 null
      prefix - the prefix to remove, may be null | 要移除的前缀,可为 null
      Returns:
      the string without the prefix, or original if no match | 移除前缀后的字符串,不匹配时返回原字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • removeSuffixIgnoreCase

      public static String removeSuffixIgnoreCase(String str, String suffix)
      Remove a suffix from a string, ignoring case. 忽略大小写移除字符串的后缀。

      Examples | 示例:

      removeSuffixIgnoreCase("HelloWorld", "world") = "Hello"
      removeSuffixIgnoreCase("HelloWorld", "WORLD") = "Hello"
      removeSuffixIgnoreCase("HelloWorld", "foo")   = "HelloWorld"
      
      Parameters:
      str - the string, may be null | 字符串,可为 null
      suffix - the suffix to remove, may be null | 要移除的后缀,可为 null
      Returns:
      the string without the suffix, or original if no match | 移除后缀后的字符串,不匹配时返回原字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • split

      public static List<String> split(String str, String separator)
      Split a string by separator, returning a List. Null-safe. 按分隔符分割字符串,返回 List。空值安全。

      Examples | 示例:

      split("a,b,c", ",")  = ["a", "b", "c"]
      split(null, ",")     = []
      split("", ",")       = [""]
      
      Parameters:
      str - the string to split, may be null | 要分割的字符串,可为 null
      separator - the separator string | 分隔符字符串
      Returns:
      a list of split parts, never null | 分割后的部分列表,不会返回 null
      Since:
      JDK 25, opencode-base-string V1.0.3
    • splitToMap

      public static Map<String,String> splitToMap(String str, String entrySep, String kvSep)
      Split a string into a key-value map using entry and key-value separators. 使用条目分隔符和键值分隔符将字符串分割为键值映射。

      Examples | 示例:

      splitToMap("a=1invalid input: '&b'=2", "invalid input: '&'", "=") = {a=1, b=2}
      splitToMap(null, "invalid input: '&'", "=")       = {}
      
      Parameters:
      str - the string to split, may be null | 要分割的字符串,可为 null
      entrySep - the separator between entries | 条目之间的分隔符
      kvSep - the separator between key and value | 键值之间的分隔符
      Returns:
      a map of key-value pairs, never null | 键值对映射,不会返回 null
      Since:
      JDK 25, opencode-base-string V1.0.3
    • join

      public static String join(String separator, Object... elements)
      Join elements with a separator. 使用分隔符合并元素。

      Examples | 示例:

      join(", ", "a", "b", "c") = "a, b, c"
      join(", ", "a", null, "c") = "a, null, c"
      
      Parameters:
      separator - the separator string | 分隔符字符串
      elements - the elements to join | 要合并的元素
      Returns:
      the joined string | 合并后的字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • joinSkipNulls

      public static String joinSkipNulls(String separator, Object... elements)
      Join elements with a separator, skipping null elements. 使用分隔符合并元素,跳过 null 元素。

      Examples | 示例:

      joinSkipNulls(", ", "a", null, "c") = "a, c"
      
      Parameters:
      separator - the separator string | 分隔符字符串
      elements - the elements to join | 要合并的元素
      Returns:
      the joined string without null elements | 跳过null后合并的字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • joinSkipBlanks

      public static String joinSkipBlanks(String separator, CharSequence... elements)
      Join elements with a separator, skipping blank elements. 使用分隔符合并元素,跳过空白元素。

      Examples | 示例:

      joinSkipBlanks(", ", "a", "", "  ", "c") = "a, c"
      
      Parameters:
      separator - the separator string | 分隔符字符串
      elements - the elements to join | 要合并的元素
      Returns:
      the joined string without blank elements | 跳过空白后合并的字符串
      Since:
      JDK 25, opencode-base-string V1.0.3
    • abbreviate

      public static String abbreviate(String str, int maxWidth)
      Abbreviate a string using ellipsis ("...") if it exceeds the maximum width. 当字符串超过最大宽度时使用省略号("...")进行简写。

      Examples | 示例:

      abbreviate("Hello World", 8) = "Hello..."
      abbreviate("Hi", 8)          = "Hi"
      abbreviate(null, 8)          = null
      
      Parameters:
      str - the string to abbreviate, may be null | 要简写的字符串,可为 null
      maxWidth - the maximum width; must be >= 4 | 最大宽度;必须 >= 4
      Returns:
      the abbreviated string | 简写后的字符串
      Throws:
      IllegalArgumentException - if maxWidth is less than 4 | 如果maxWidth小于4
      Since:
      JDK 25, opencode-base-string V1.0.3
    • abbreviate

      public static String abbreviate(String str, int offset, int maxWidth)
      Abbreviate a string using ellipsis ("...") starting from an offset. 从指定偏移位置开始使用省略号("...")简写字符串。

      If the offset is greater than 0 and the string needs abbreviation, the result may start with "..." to indicate truncation from the left.

      如果偏移量大于0且字符串需要简写,结果可能以"..."开头表示左侧被截断。

      Examples | 示例:

      abbreviate("Hello World Test", 0, 11) = "Hello Wo..."
      abbreviate("Hello World Test", 6, 11) = "...orld ..."
      abbreviate(null, 0, 8)                = null
      
      Parameters:
      str - the string to abbreviate, may be null | 要简写的字符串,可为 null
      offset - the left edge of the source string; if positive and truncation is needed, prepends "..." | 源字符串的左边缘偏移
      maxWidth - the maximum width; must be >= 4 | 最大宽度;必须 >= 4
      Returns:
      the abbreviated string | 简写后的字符串
      Throws:
      IllegalArgumentException - if maxWidth is less than 4 | 如果maxWidth小于4
      Since:
      JDK 25, opencode-base-string V1.0.3