Class OpenString
java.lang.Object
cloud.opencode.base.string.OpenString
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 Summary
Modifier and TypeMethodDescriptionstatic StringabbreviateMiddle(String str, String middle, int maxLength) static Stringcapitalize(String str) static StringCenter string within the given minimum length using the pad character.charFrequency(String str) static Stringstatic Stringstatic StringcommonPrefix(String... strs) static StringcommonSuffix(String... strs) static booleancontainsChinese(String str) static intcountMatches(String str, char ch) static intcountMatches(String str, String subStr) static StringdefaultIfBlank(String str, String defaultStr) static StringdefaultIfEmpty(String str, String defaultStr) static Stringdifference(String str1, String str2) static StringensurePrefix(String str, String prefix) static StringensureSuffix(String str, String suffix) static StringensureWrap(String str, String wrap) static StringfirstNonBlank(String... strs) static StringfirstNonEmpty(String... strs) static Stringstatic StringgetRepeatedPattern(String str) static intindexOfDifference(String str1, String str2) static intindexOfNth(String str, String subStr, int n) static booleanisAllChinese(String str) static booleanisAllLowerCase(String str) static booleanisAllUpperCase(String str) static booleanstatic booleanisAlphanumeric(String str) static booleanstatic booleanisMixedCase(String str) static booleanstatic booleanisPalindrome(String str) static booleanstatic booleanisRepeated(String str) static StringkeepAlphanumeric(String str) static StringkeepChinese(String str) static StringkeepDigits(String str) static StringkeepLetters(String str) static intlastIndexOfNth(String str, String subStr, int n) static StringGet the leftmostlengthcharacters of a string.static Stringstatic Stringstatic StringPad string on the left to reach minimum length.static StringPad string on the right to reach minimum length.static Stringstatic StringremovePrefix(String str, String prefix) static StringremoveSpecialChars(String str) static StringremoveSuffix(String str, String suffix) static StringremoveWhitespace(String str) static Stringstatic StringGet the rightmostlengthcharacters of a string.static Stringstatic Stringstatic Stringstatic StringstripAccents(String str) static StringsubstringAfter(String str, String separator) static StringsubstringAfterLast(String str, String separator) static StringsubstringBefore(String str, String separator) static StringsubstringBeforeLast(String str, String separator) static StringsubstringBetween(String str, String open, String close) static String[]substringsBetween(String str, String open, String close) static Stringstatic StringtoTitleCase(String str) static Stringstatic Stringstatic StringtruncateByBytes(String str, int maxBytes, String charset) static StringtruncateMiddle(String str, int maxLength) static Stringuncapitalize(String str) static Stringstatic StringwordFrequency(String str) static Stringstatic String
-
Method Details
-
padLeft
Pad string on the left to reach minimum length. 在字符串左侧填充以达到最小长度。- Parameters:
str- the input string, may benull(returnsnullif so)minLength- the minimum desired length; must not be negativepadChar- the character to pad with- Returns:
- the padded string, or
nullif input wasnull - Throws:
IllegalArgumentException- if minLength is negative
-
padRight
Pad string on the right to reach minimum length. 在字符串右侧填充以达到最小长度。- Parameters:
str- the input string, may benull(returnsnullif so)minLength- the minimum desired length; must not be negativepadChar- the character to pad with- Returns:
- the padded string, or
nullif input wasnull - Throws:
IllegalArgumentException- if minLength is negative
-
center
Center string within the given minimum length using the pad character. 使用填充字符将字符串居中至给定的最小长度。- Parameters:
str- the input string, may benull(returnsnullif so)minLength- the minimum desired length; must not be negativepadChar- the character to pad with- Returns:
- the centered string, or
nullif input wasnull - Throws:
IllegalArgumentException- if minLength is negative
-
left
Get the leftmostlengthcharacters of a string. 获取字符串最左侧的length个字符。- Parameters:
str- the input string, may benull(returnsnullif so)length- the number of characters to extract; negative values returnnullinput unchanged- Returns:
- the leftmost characters, or
nullif input wasnull
-
right
Get the rightmostlengthcharacters of a string. 获取字符串最右侧的length个字符。- Parameters:
str- the input string, may benull(returnsnullif so)length- the number of characters to extract; negative values returnnullinput unchanged- Returns:
- the rightmost characters, or
nullif input wasnull
-
mid
-
truncate
-
truncate
-
truncateMiddle
-
truncateByBytes
-
capitalize
-
uncapitalize
-
swapCase
-
toTitleCase
-
reverse
-
shuffle
-
shuffle
-
countMatches
-
countMatches
-
charFrequency
-
wordFrequency
-
removeWhitespace
-
normalizeWhitespace
-
removeInvisibleChars
-
removeSpecialChars
-
keepDigits
-
keepLetters
-
keepAlphanumeric
-
keepChinese
-
isNumeric
-
isAlpha
-
isAlphanumeric
-
isAscii
-
containsChinese
-
isAllChinese
-
isAllLowerCase
-
isAllUpperCase
-
isMixedCase
-
isPalindrome
-
isPalindromeIgnoreCase
-
ensurePrefix
-
ensureSuffix
-
ensureWrap
-
removePrefix
-
removeSuffix
-
commonPrefix
-
commonSuffix
-
findAll
-
indexOfNth
-
lastIndexOfNth
-
substringBefore
-
substringAfter
-
substringBeforeLast
-
substringAfterLast
-
substringBetween
-
substringsBetween
-
wrap
-
wrap
-
unwrap
-
unwrap
-
chomp
-
chop
-
stripAccents
-
getDigits
-
rotate
-
difference
-
indexOfDifference
-
abbreviateMiddle
-
isRepeated
-
getRepeatedPattern
-
defaultIfBlank
-
defaultIfEmpty
-
firstNonBlank
-
firstNonEmpty
-