Class StringUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic PatterncompileMatchPattern(String match) Builds aPatternfrom a simple match string with shell-style wildcards, then compiles it withPattern.CASE_INSENSITIVEandPattern.DOTALL.static StringReads all bytes frominuntil end-of-stream and decodes them as UTF-8.static StringconvertInputStreamToString(InputStream in, Charset charset) Reads all bytes frominuntil end-of-stream and decodes them withcharset.static booleanisDigits(CharSequence input) Returns whetherinputis an optional leading'-'followed only by ASCII digits'0'–'9'.static booleanisLowerCase(String input) Returns whetherinputequals itsString.toLowerCase()form using the default locale.static booleanisNumeric(CharSequence input) Returns whetherinputlooks like a signed decimal integer: an optional leading ASCII hyphen ('-') followed only by characters for whichCharacter.isDigit(char)istrue.static booleanisUpperCase(String input) Returns whetherinputequals itsString.toUpperCase()form using the default locale.static <T> StringJoins elements withString.valueOf(Object)for non-null items;nullelements are represented as the four charactersnull, same asString.join(separator, elements)for anIterableofCharSequence.static <T> Stringjoin(Iterable<T> items, String separator, Function<? super T, ? extends CharSequence> toString) Joins elements usingtoStringfor non-null items;nullelements are not passed totoString— they are joined likeString.join(CharSequence, Iterable)(the literalnull).static StringJoins multiple values into one string, dispatching on the runtime shape ofobj.static StringremoveLineBreaks(String text, boolean removeSoftHyphens) Normalizes line breaks intextfor a single-line or flowed paragraph style.splitAndTrim(String string, String regex) SplitsstringwithString.split(String, int)usinglimit0(trailing empty segments discarded perString.split(String)), trims each segment withString.trim(), and omits segments that are blank after trimming.splitAndTrim(String string, String regex, int limit) Same assplitAndTrim(String, String)but forwardslimittoString.split(String, int)so callers can cap splits or preserve trailing empties whenlimitis negative or greater than zero, perString.split(String, int).splitLines(String input) static StringtoCamelCase(String input) Converts a phrase or identifier to lowercamelCase: first token lowercased, rest title-cased and concatenated.static StringtoKebabCase(String input) Converts a phrase or identifier tokebab-case: tokens lowercased and joined with'-'.static StringtoSnakeCase(String input) Converts a phrase or identifier tosnake_case: tokens lowercased and joined with'_'.static StringReturns a readable string forvalue, including array contents.
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
toString
Returns a readable string forvalue, including array contents.nullbecomes the literal"null". Non-array values useString.valueOf(Object). Primitive arrays use the matchingArrays.toStringoverload; reference-type arrays useArrays.deepToString(Object[])so nested arrays are expanded instead of default object references like[I@….- Parameters:
value- any object or array, ornull- Returns:
- text suitable for logging or default formatting
-
join
Joins elements withString.valueOf(Object)for non-null items;nullelements are represented as the four charactersnull, same asString.join(separator, elements)for anIterableofCharSequence.- Parameters:
items- sequence to join; not null (may be empty)separator- placed between elements; not null (may be empty)
-
join
public static <T> String join(Iterable<T> items, String separator, Function<? super T, ? extends CharSequence> toString) Joins elements usingtoStringfor non-null items;nullelements are not passed totoString— they are joined likeString.join(CharSequence, Iterable)(the literalnull). IftoStringreturnsnull, that is passed toStringJoiner.add(CharSequence)and becomes the same four characters.- Parameters:
items- sequence to join; not null (may be empty)separator- placed between elements; not null (may be empty)toString- converts each non-null element; not null
-
join
Joins multiple values into one string, dispatching on the runtime shape ofobj.If
objis anIterable, each element is appended withString.valueOf(Object). If it is any array (including primitives), elements are indexed withArray.get(java.lang.Object, int). If it is anIterator, remaining elements are consumed and appended (the iterator is advanced). If it is aMap, eachMap.Entrybecomeskey=valuevia string concatenation. Otherwise a single segmentString.valueOf(Object)is used.- Parameters:
obj- non-null container, array, iterator, map, or scalarseparator- placed between segments; not null (may be empty)
-
splitLines
- Parameters:
input- the text; must not benull- Returns:
- a list of lines (may contain a single element for text without newlines; behavior matches
String.split(java.lang.String, int)) - Throws:
NullPointerException- ifinputisnull
-
splitAndTrim
SplitsstringwithString.split(String, int)usinglimit0(trailing empty segments discarded perString.split(String)), trims each segment withString.trim(), and omits segments that are blank after trimming.- Parameters:
string- text to split; may benull(returns an empty list)regex- delimiter pattern passed toString.split(String, int); must not benull- Returns:
- a new list of non-blank trimmed parts (never
null; may be empty) - Throws:
NullPointerException- ifregexisnull- See Also:
-
splitAndTrim
Same assplitAndTrim(String, String)but forwardslimittoString.split(String, int)so callers can cap splits or preserve trailing empties whenlimitis negative or greater than zero, perString.split(String, int).- Parameters:
string- text to split; may benull(returns an empty list)regex- delimiter pattern; must not benulllimit- maximum number of parts; same semantics asString.split(String, int)- Returns:
- a new list of non-blank trimmed parts (never
null; may be empty) - Throws:
NullPointerException- ifregexisnull- See Also:
-
removeLineBreaks
Normalizes line breaks intextfor a single-line or flowed paragraph style.Applied in order:
- Strips leading spaces and tabs on each line (after a newline or at the start) and trailing spaces and tabs before a newline or end of string.
- Removes a newline immediately after a hyphen-like character (ASCII hyphen, Unicode dashes, or soft hyphen U+00AD), keeping the hyphen (typical line-break hyphenation in plain text).
- Replaces remaining newlines that sit between two non-newline characters with a single space
(joins wrapped lines). Newlines adjacent to other line breaks are left as-is because the
lookahead/lookbehind use
., which does not match line terminators.
If
removeSoftHyphensistrue, all soft hyphens (U+00AD) are removed from the result.- Parameters:
text- source text; must not benullremoveSoftHyphens- whentrue, strips every U+00AD from the result- Returns:
- normalized text with the rules above applied
- Throws:
NullPointerException- iftextisnull
-
isNumeric
Returns whetherinputlooks like a signed decimal integer: an optional leading ASCII hyphen ('-') followed only by characters for whichCharacter.isDigit(char)istrue.nulland the empty sequence returnfalse. A lone"-"is not numeric (there must be at least one digit after an optional sign).- Parameters:
input- text to inspect; may benull- Returns:
trueif non-empty and every character passes the rules above
-
isDigits
Returns whetherinputis an optional leading'-'followed only by ASCII digits'0'–'9'.null, the empty sequence, and a lone"-"returnfalse.- Parameters:
input- text to inspect; may benull- Returns:
trueif non-empty and every character passes the rules above
-
compileMatchPattern
Builds aPatternfrom a simple match string with shell-style wildcards, then compiles it withPattern.CASE_INSENSITIVEandPattern.DOTALL.Transformations apply only to characters that are not escaped by a preceding backslash (per-step
(?<!\\\\)rules in the implementation):- Runs of paired backslashes are replaced by a single
*. - Redundant unescaped
?/*clusters (e.g.?**) collapse to one*. - Remaining regex metacharacters in
|[]{}(),.^$+-are escaped for literal matching. - Unescaped
?becomes(.?)(zero or one of any character, including newlines). - Unescaped
*becomes(.*)(any length, including newlines).
- Parameters:
match- user-facing pattern; must not benull- Returns:
- compiled pattern ready for
Matcher.matches(),Matcher.find(), etc. - Throws:
NullPointerException- ifmatchisnullPatternSyntaxException- if the transformed string is not a valid regex
- Runs of paired backslashes are replaced by a single
-
isUpperCase
Returns whetherinputequals itsString.toUpperCase()form using the default locale.- Parameters:
input- the string; must not benull- Returns:
trueif unchanged bytoUpperCase()- Throws:
NullPointerException- ifinputisnull
-
isLowerCase
Returns whetherinputequals itsString.toLowerCase()form using the default locale.- Parameters:
input- the string; must not benull- Returns:
trueif unchanged bytoLowerCase()- Throws:
NullPointerException- ifinputisnull
-
convertInputStreamToString
Reads all bytes frominuntil end-of-stream and decodes them as UTF-8.Does not close
in.- Parameters:
in- the stream; must not benull- Returns:
- the full decoded string
- Throws:
IOException- if reading fails
-
convertInputStreamToString
Reads all bytes frominuntil end-of-stream and decodes them withcharset.Does not close
in.- Parameters:
in- the stream; must not benullcharset- character set for decoding; must not benull- Returns:
- the full decoded string
- Throws:
IOException- if reading fails
-
toCamelCase
Converts a phrase or identifier to lowercamelCase: first token lowercased, rest title-cased and concatenated.Uses
CASE_CONVENTION_TOKEN_PATTERNto segment the input.- Parameters:
input- the source string; must not benull- Returns:
- camel-cased text
- Throws:
NullPointerException- ifinputisnullStringIndexOutOfBoundsException- if no tokens match (e.g.inputis empty)
-
toKebabCase
Converts a phrase or identifier tokebab-case: tokens lowercased and joined with'-'.Uses
CASE_CONVENTION_TOKEN_PATTERNto segment the input.- Parameters:
input- the source string; must not benull- Returns:
- kebab-cased text (empty if
inputis empty) - Throws:
NullPointerException- ifinputisnull
-
toSnakeCase
Converts a phrase or identifier tosnake_case: tokens lowercased and joined with'_'.Uses
CASE_CONVENTION_TOKEN_PATTERNto segment the input.- Parameters:
input- the source string; must not benull- Returns:
- snake-cased text (empty if
inputis empty) - Throws:
NullPointerException- ifinputisnull
-