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)