Class StringUtil
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcapitalizeFirst(String text, Locale locale) static <T extends Comparable<T>>
intcompareToNullable(T v1, T v2) Compares two objects of type T that may be null.static <T extends Comparable<T>>
intcompareToWithNulls(T v1, T v2) Deprecated.static StringcompressSpaces(String str) Compresses spaces in case of non-preformatting paragraph.convertToList(String str) For a string containing a space-separated list of items, convert that string into an ArrayListstatic StringdecodeBase64(String b64data, Charset charset) Decode the Base64-encodedcharsetbytes back to a String.static StringencodeBase64(char[] chars, Charset charset) Convert a char array'scharsetbytes into a Base64-encoded String.static StringencodeBase64(String string, Charset charset) Convert a string'scharsetbytes into a Base64-encoded String.static booleanCompares two strings for equality.static StringescapeXMLChars(int cp) static StringExtracts first N codepoints from string.static StringFormats UI strings.static intReturns first letter in lowercase.static StringgetTailSegments(@NotNull String str, int separator, int segments) For a string delimited by some separator, retrieve the lastsegmentssegments.static booleanstatic booleanCheck if string is empty, i.e.static booleanisLowerCase(String input) Returns true if the input has at least one letter and all letters are lower case.static booleanisMixedCase(String input) Returns true if the input has both upper case and lower case letters, but is not title case.static booleanisSubstringAfter(String text, int pos, String substring) Checks if text contains substring after specified position.static booleanisSubstringBefore(String text, int pos, String substring) Checks if text contains substring before specified position.static booleanisTitleCase(int codePoint) static booleanisTitleCase(String input) Returns true if the input is title case, meaning the first character is UpperCase or TitleCase* and the rest of the string (if present) is LowerCase.static booleanisUpperCase(String input) Returns true if the input is upper case.static booleanisValidXMLChar(int codePoint) Determines whether the provided code point is a valid XML character.static booleanisWhiteSpace(int codePoint) Returns true if the input is a whitespace character (including non-breaking characters that are false according toCharacter.isWhitespace(int)).static booleanisWhiteSpace(String input) Returns true if the input consists only of whitespace characters (including non-breaking characters that are false according toCharacter.isWhitespace(int)).static StringmakeValidXML(String plaintext) Converts a stream of plaintext into valid XML.static StringmatchCapitalization(String text, String matchTo, Locale locale) static StringnormalizeUnicode(CharSequence text) Apply Unicode NFC normalization to a string.static StringnormalizeWidth(String text) Normalizes the width of characters in the given text to ensure consistency in character forms.static <T> Tnvl(@Nullable T... values) Returns first not null object from list, or null if all values is null.static longnvlLong(long... values) Returns first non-zero object from list, or zero if all values is null.static StringReplace invalid XML chars by spaces.static StringreplaceCase(@NotNull String input, Locale locale) Interpret the case replacement language used in regular expressions: backslash u = uppercase next letter backslash l = lowercase next letter backslash U = uppercase next letters until backslash E or end backslash L = lowercase next letters until backslash E or end backslash u + backslash L = uppercase next letter then lowercase all until backslash E backslash l + backslash U = lowercase next letter then uppercase all until backslash Estatic StringStrip whitespace from the end of a string.static StringstripFromEnd(String string, String... toStrip) static StringtoTitleCase(String text, Locale locale) Convert text to title case according to the supplied locale.static StringTruncate the supplied text to a maximum of len codepoints.static Stringstatic StringunescapeXMLEntities(String text) Converts XML entities to characters.static StringWrap line by length.
-
Field Details
-
TRUNCATE_CHAR
public static final char TRUNCATE_CHAR- See Also:
-
-
Method Details
-
isEmpty
Check if string is empty, i.e. null or length==0 -
isLowerCase
Returns true if the input has at least one letter and all letters are lower case. -
isUpperCase
Returns true if the input is upper case. -
isMixedCase
Returns true if the input has both upper case and lower case letters, but is not title case. -
isTitleCase
Returns true if the input is title case, meaning the first character is UpperCase or TitleCase* and the rest of the string (if present) is LowerCase.*There are exotic characters that are neither UpperCase nor LowerCase, but are TitleCase: e.g. LATIN CAPITAL LETTER L WITH SMALL LETTER J (U+01C8)
These are handled correctly. -
isTitleCase
public static boolean isTitleCase(int codePoint) -
isWhiteSpace
Returns true if the input consists only of whitespace characters (including non-breaking characters that are false according toCharacter.isWhitespace(int)). -
isWhiteSpace
public static boolean isWhiteSpace(int codePoint) Returns true if the input is a whitespace character (including non-breaking characters that are false according toCharacter.isWhitespace(int)). -
isCJK
-
capitalizeFirst
-
replaceCase
Interpret the case replacement language used in regular expressions:- backslash u = uppercase next letter
- backslash l = lowercase next letter
- backslash U = uppercase next letters until backslash E or end
- backslash L = lowercase next letters until backslash E or end
- backslash u + backslash L = uppercase next letter then lowercase all until backslash E
- backslash l + backslash U = lowercase next letter then uppercase all until backslash E
Warning: This method works directly with the input string. Perform other substitutions (e.g., variable conversion) before calling this method; otherwise, unconverted substitutions will not receive proper case handling.
-
matchCapitalization
-
toTitleCase
Convert text to title case according to the supplied locale. -
nvl
Returns first not null object from list, or null if all values is null. -
nvlLong
public static long nvlLong(long... values) Returns first non-zero object from list, or zero if all values is null. -
compareToWithNulls
Deprecated.Compares two objects of type T that may be null.- Type Parameters:
T- the type of objects to be compared, which must implement Comparable- Parameters:
v1- the first object to compare, which may be nullv2- the second object to compare, which may be null- Returns:
- a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
-
compareToNullable
Compares two objects of type T that may be null.Compares two nullable values of a type that extends
Comparable. Handlesnullvalues by consideringnullas less than any non-null value. If both values arenull, they are considered equal.- Type Parameters:
T- The type of the values being compared, which must implementComparable.- Parameters:
v1- The first value to compare. May benull.v2- The second value to compare. May benull.- Returns:
- A negative integer, zero, or a positive integer if
v1is less than, equal to, or greater thanv2, respectively.
-
firstN
Extracts first N codepoints from string. -
truncate
Truncate the supplied text to a maximum of len codepoints. If truncated, the result will be the first (len - 1) codepoints plus a trailing ellipsis.- Parameters:
text- The text to truncatelen- The desired length (in codepoints) of the result- Returns:
- The truncated string
-
getFirstLetterLowercase
Returns first letter in lowercase. Usually used for create tag shortcuts. -
isSubstringAfter
Checks if text contains substring after specified position. -
isSubstringBefore
Checks if text contains substring before specified position. -
stripFromEnd
-
normalizeUnicode
Apply Unicode NFC normalization to a string. -
removeXMLInvalidChars
Replace invalid XML chars by spaces.- Parameters:
str- input stream- Returns:
- result stream
- See Also:
-
isValidXMLChar
public static boolean isValidXMLChar(int codePoint) Determines whether the provided code point is a valid XML character.- Parameters:
codePoint- the code point to validate- Returns:
- true if the code point is a valid XML character, otherwise false
-
makeValidXML
Converts a stream of plaintext into valid XML. Output stream must convert stream to UTF-8 when saving to disk. -
compressSpaces
Compresses spaces in case of non-preformatting paragraph. -
escapeXMLChars
-
unescapeXMLEntities
Converts XML entities to characters. -
equal
Compares two strings for equality. Handles nulls: if both strings are nulls they are considered equal. -
format
Formats UI strings.Note: This is only a first attempt at putting right what goes wrong in MessageFormat. Currently it only duplicates single quotes, but it doesn't even test if the string contains parameters (numbers in curly braces), and it doesn't allow for string containg already escaped quotes.
- Parameters:
str- The string to formatarguments- Arguments to use in formatting the string- Returns:
- The formatted string
-
normalizeWidth
Normalizes the width of characters in the given text to ensure consistency in character forms.The normalization applies specific transformations based on character types:
- ASCII characters are converted to their halfwidth forms.
- Katakana characters are converted to their fullwidth forms.
- Hangul characters are converted to their fullwidth forms.
- Letter-like symbols and squared Latin abbreviations are decomposed into their ASCII equivalents.
This method improves modularity and efficiency by replacing the large switch-case structure used in the original implementation with a table-based look-up approach.
Originally adapted from FullWidthConversionStep.java in the Okapi Framework licensed under GPLv2+.
- Parameters:
text- the input text to normalize- Returns:
- the text with normalized-width characters
-
rstrip
Strip whitespace from the end of a string. UsesCharacter.isWhitespace(int), so it does not strip the extra non-breaking whitespace included inisWhiteSpace(int).- Parameters:
text- the text to strip- Returns:
- text with trailing whitespace removed
-
encodeBase64
Convert a string'scharsetbytes into a Base64-encoded String.- Parameters:
string- a stringcharset- the charset with which to obtain the bytes- Returns:
- Base64-encoded String
-
encodeBase64
Convert a char array'scharsetbytes into a Base64-encoded String. Useful for handling passwords. Intermediate buffers are cleared after use.- Parameters:
chars- a char arraycharset- the charset with which to obtain the bytes- Returns:
- Base64-encoded String
-
decodeBase64
Decode the Base64-encodedcharsetbytes back to a String.- Parameters:
b64data- Base64-encoded Stringcharset- charset of decoded bytes- Returns:
- String
-
getTailSegments
For a string delimited by some separator, retrieve the lastsegmentssegments.- Parameters:
str- The stringseparator- The separator delimiting the string's segmentssegments- The number of segments to return, starting at the end- Returns:
- The trailing segments, or, if
segmentsis greater than the number of segments contained instr, thenstritself.
-
convertToList
For a string containing a space-separated list of items, convert that string into an ArrayList- Parameters:
str- The string, with items separated by whitespace- Returns:
- An ArrayList of the items in the original space-separated list
-
wrap
Wrap line by length.- Parameters:
text- string to process.length- wrap length.- Returns:
- string wrapped.
-
unescapeLinefeed
-