Class StringUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThis is an enumeration of the ways in which an input string may be split by thesplit*(…)methods. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charThe prefix that is recognised by theescape(CharSequence, String)method. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringapplyPrefix(String str, String prefix) Returns a string that is derived from the specified string by applying the specified prefix in a way that depends on whether or not the prefix ends with an underscore ('_', U+005F): If the prefix ends with an underscore, the returned string consists of the prefix followed by the input string.static booleancontainsIgnoreCase(String target, Iterable<String> strs) Returnstrueif the specified collection of strings contains the specified target string, ignoring letter case when comparing strings.static Stringescape(CharSequence seq, String metachars) Returns a string that consists of the specified character sequence with each occurrence of any of the specified set of metacharacters immediately preceded by a backslash ('\', U+005C).extractLines(CharSequence text) Splits the specified text at line separators and returns the resulting list of lines.static StringReturns a string that is derived from the specified string by converting its first character to lower case.static StringReturns a string that is derived from the specified string by converting its first character to upper case.static intgetMaxLength(CharSequence... seqs) Returns the maximum length of the specified character sequences.static intgetMaxLength(Iterable<? extends CharSequence> seqs) Returns the maximum length of the specified character sequences.static StringgetPrefixFirst(String str, char ch) Returns the substring of the specified string from the start of the string to (but not including) the first occurrence of the specified character.static StringgetPrefixLast(String str, char ch) Returns the substring of the specified string from the start of the string to (but not including) the last occurrence of the specified character.static StringgetSuffixAfterFirst(String str, char ch) Returns the substring of the specified string from (but not including) the first occurrence of the specified character to the end of the string.static StringgetSuffixAfterLast(String str, char ch) Returns the substring of the specified string from (but not including) the last occurrence of the specified character to the end of the string.static StringgetSuffixFirst(String str, char ch) Returns the substring of the specified string from (and including) the first occurrence of the specified character to the end of the string.static StringgetSuffixLast(String str, char ch) Returns the substring of the specified string from (and including) the last occurrence of the specified character to the end of the string.static intindexOfIgnoreCase(String target, Iterable<String> strs) Returns the index of the first occurrence of the specified target string in the specified collection of strings, ignoring letter case when comparing strings.static booleanisNullOrBlank(String str) static booleanisNullOrEmpty(String str) Returnstrueif the specified string isnullor empty.static Stringjoin(char separator, boolean includeTrailingSeparator, CharSequence... seqs) Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences and an optional separator after the last character sequence.static Stringjoin(char separator, boolean includeTrailingSeparator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences and an optional separator after the last character sequence.static Stringjoin(char separator, CharSequence... seqs) Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences.static Stringjoin(char separator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences.static Stringjoin(CharSequence separator, boolean includeTrailingSeparator, CharSequence... seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences and an optional separator after the last character sequence.static Stringjoin(CharSequence separator, boolean includeTrailingSeparator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences and an optional separator after the last character sequence.static Stringjoin(CharSequence separator, CharSequence... seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences.static Stringjoin(CharSequence separator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences.static StringpadAfter(CharSequence seq, int length) Returns the specified character sequence as a string that is padded with trailing spaces (U+0020), if necessary, so that the string has the specified length.static StringpadAfter(CharSequence seq, int length, char ch) Returns the specified character sequence as a string that is padded at the end with repetitions of the specified character, if necessary, so that the string has the specified length.static StringpadBefore(CharSequence seq, int length) Returns the specified character sequence as a string that is padded with leading spaces (U+0020), if necessary, so that the string has the specified length.static StringpadBefore(CharSequence seq, int length, char ch) Returns the specified character sequence as a string that is padded at the start with repetitions of the specified character, if necessary, so that the string has the specified length.static StringremovePrefix(String str, String prefix) Removes the specified prefix (ie, leading substring) from the specified string, if the string starts with the prefix, and returns the resulting string.static StringremoveSuffix(String str, String suffix) Removes the specified suffix (ie, trailing substring) from the specified string, if the string ends with the suffix, and returns the resulting string.Returns a list of the substrings of the specified string that result from splitting the input string at each occurrence of the specified separator character and removing the separators.Returns a list of the substrings of the specified string that result from splitting the input string at each occurrence of the specified separator character and removing the separators.static String[]splitAt(String str, int index, StringUtils.SplitMode splitMode) Returns the two substrings that result from splitting the specified string at the specified index.static String[]splitAtFirst(String str, char ch) Returns the two substrings that result from splitting the specified string at the first occurrence of the specified character, which is discarded.static String[]splitAtFirst(String str, char ch, StringUtils.SplitMode splitMode) Returns the two substrings that result from splitting the specified string at the first occurrence of the specified character, which may optionally bestatic String[]splitAtLast(String str, char ch) Returns the two substrings that result from splitting the specified string at the last occurrence of the specified character, which is discarded.static String[]splitAtLast(String str, char ch, StringUtils.SplitMode splitMode) Returns the two substrings that result from splitting the specified string at the last occurrence of the specified character, which may optionally bestatic StringtoCamelCase(CharSequence text) Converts the specified text to camel case and returns the resulting string.static Stringwrap(CharSequence seq, int maxLineLength) Returns the string that results from splitting the specified character sequence into substrings at word breaks that maximise the length of each substring (with the word break removed) while not exceeding the specified maximum length, then joining adjacent substrings with a line feed (U+000A).wrapLines(CharSequence seq, int maxLineLength) Returns the list of strings that results from splitting the specified character sequence into substrings at word breaks that maximise the length of each substring (with the word break removed) while not exceeding the specified maximum length.
-
Field Details
-
ESCAPE_PREFIX_CHAR
public static final char ESCAPE_PREFIX_CHARThe prefix that is recognised by theescape(CharSequence, String)method.- See Also:
-
-
Method Details
-
isNullOrEmpty
Returnstrueif the specified string isnullor empty.- Parameters:
str- the string that will be tested.- Returns:
trueifstrisnullor empty.
-
isNullOrBlank
- Parameters:
str- the string that will be tested.- Returns:
trueifstrisnullor empty or it contains only whitespace characters.
-
getMaxLength
Returns the maximum length of the specified character sequences.- Parameters:
seqs- the character sequences whose maximum length is desired.- Returns:
- the maximum length of
seqs.
-
getMaxLength
Returns the maximum length of the specified character sequences.- Parameters:
seqs- the character sequences whose maximum length is desired.- Returns:
- the maximum length of
seqs.
-
padBefore
Returns the specified character sequence as a string that is padded with leading spaces (U+0020), if necessary, so that the string has the specified length. If the length of the input sequence is greater than or equal to the specified length, the input sequence is returned as a string with no additional padding.- Parameters:
seq- the input sequence of characters.length- the desired length of the padded output string.- Returns:
seqas a string, padded with leading spaces, if necessary, so that the length of the returned string islength.
-
padBefore
Returns the specified character sequence as a string that is padded at the start with repetitions of the specified character, if necessary, so that the string has the specified length. If the length of the input sequence is greater than or equal to the specified length, the input sequence is returned as a string with no additional padding.- Parameters:
seq- the input sequence of characters.length- the desired length of the padded output string.ch- the character with which the string will be padded.- Returns:
seqas a string, padded at the start with repetitions ofch, if necessary, so that the length of the returned string islength.
-
padAfter
Returns the specified character sequence as a string that is padded with trailing spaces (U+0020), if necessary, so that the string has the specified length. If the length of the input sequence is greater than or equal to the specified length, the input sequence is returned as a string with no additional padding.- Parameters:
seq- the input sequence of characters.length- the desired length of the padded output string.- Returns:
seqas a string, padded with trailing spaces, if necessary, so that the length of the returned string islength.
-
padAfter
Returns the specified character sequence as a string that is padded at the end with repetitions of the specified character, if necessary, so that the string has the specified length. If the length of the input sequence is greater than or equal to the specified length, the input sequence is returned as a string with no additional padding.- Parameters:
seq- the input sequence of characters.length- the desired length of the padded output string.ch- the character with which the string will be padded.- Returns:
seqas a string, padded at the end with repetitions ofch, if necessary, so that the length of the returned string islength.
-
split
Returns a list of the substrings of the specified string that result from splitting the input string at each occurrence of the specified separator character and removing the separators.- Parameters:
str- the string that is to be split.separator- the separator character.- Returns:
- a list of the substrings of
strthat result from splitting the input string at each occurrence ofseparator.
-
split
Returns a list of the substrings of the specified string that result from splitting the input string at each occurrence of the specified separator character and removing the separators. The last element of the list of substrings may optionally be discarded if it is the empty string.- Parameters:
str- the string that is to be split.separator- the separator character.discardFinalEmptyElement- iftrue, the last element of the list of substrings will be discarded if it is the empty string.- Returns:
- a list of the substrings of
strthat result from splitting the input string at each occurrence ofseparatorand optionally discarding the last element if it is empty.
-
splitAt
Returns the two substrings that result from splitting the specified string at the specified index. The character at the specified index in the input string may optionally be
- included in the first substring (the prefix),
- included in the second substring (the suffix) or
- discarded,
according to the specified split mode.
- Parameters:
str- the string that is to be split.index- the index of the character at whichstris to be split. If it is less than 0, the result is as described below.splitMode- the way in which the character ofstratindexwill be treated.- Returns:
- an array containing the two substrings that result from splitting
stratindex, taking into accountsplitMode. Ifindexis less than 0, the two returned values will bestrandnull, ifsplitModeisNONE, orstrand the empty string, ifsplitModeisPREFIXorSUFFIX.
-
splitAtFirst
Returns the two substrings that result from splitting the specified string at the first occurrence of the specified character, which is discarded.- Parameters:
str- the string that is to be split.ch- the character at whichstris to be split.- Returns:
- an array containing the two substrings that result from splitting
strat the first occurrence ofch. Ifstrdoes not containch, the two returned values will bestrandnull.
-
splitAtFirst
Returns the two substrings that result from splitting the specified string at the first occurrence of the specified character, which may optionally be
- included in the first substring (the prefix),
- included in the second substring (the suffix) or
- discarded,
according to the specified split mode.
- Parameters:
str- the string that is to be split.ch- the character at whichstris to be split.splitMode- the way in which the first occurrence ofchwill be treated.- Returns:
- an array containing the two substrings that result from splitting
strat the first occurrence ofch. Ifstrdoes not containch, the two returned values will bestrandnull, ifsplitModeisNONE, orstrand the empty string, ifsplitModeisPREFIXorSUFFIX.
-
splitAtLast
Returns the two substrings that result from splitting the specified string at the last occurrence of the specified character, which is discarded.- Parameters:
str- the string that is to be split.ch- the character at whichstris to be split.- Returns:
- an array containing the two substrings that result from splitting
strat the last occurrence ofch. Ifstrdoes not containch, the two returned values will bestrandnull.
-
splitAtLast
Returns the two substrings that result from splitting the specified string at the last occurrence of the specified character, which may optionally be
- included in the first substring (the prefix),
- included in the second substring (the suffix) or
- discarded,
according to the specified split mode.
- Parameters:
str- the string that is to be split.ch- the character at whichstris to be split.splitMode- the way in which the last occurrence ofchwill be treated.- Returns:
- an array containing the two substrings that result from splitting
strat the last occurrence ofch. Ifstrdoes not containch, the two returned values will bestrandnull, ifsplitModeisNONE, orstrand the empty string, ifsplitModeisPREFIXorSUFFIX.
-
getPrefixFirst
Returns the substring of the specified string from the start of the string to (but not including) the first occurrence of the specified character.- Parameters:
str- the string from which a leading substring is to be extracted.ch- the character whose first occurrence denotes the exclusive end of the prefix.- Returns:
- the substring of
strfrom the start of the string to (but not including) the first occurrence ofch. Ifstrdoes not containch,stris returned.
-
getPrefixLast
Returns the substring of the specified string from the start of the string to (but not including) the last occurrence of the specified character.- Parameters:
str- the string from which a leading substring is to be extracted.ch- the character whose last occurrence denotes the exclusive end of the prefix.- Returns:
- the substring of
strfrom the start of the string to (but not including) the last occurrence ofch. Ifstrdoes not containch,stris returned.
-
getSuffixFirst
Returns the substring of the specified string from (and including) the first occurrence of the specified character to the end of the string.- Parameters:
str- the string from which a trailing substring is to be extracted.ch- the character whose first occurrence denotes the inclusive start of the suffix.- Returns:
- the substring of
strfrom (and including) the first occurrence ofchto the end of the string. Ifstrdoes not containch,stris returned.
-
getSuffixAfterFirst
Returns the substring of the specified string from (but not including) the first occurrence of the specified character to the end of the string.- Parameters:
str- the string from which a trailing substring is to be extracted.ch- the character whose first occurrence denotes the exclusive start of the suffix.- Returns:
- the substring of
strfrom (but not including) the first occurrence ofchto the end of the string. Ifstrdoes not containch,stris returned.
-
getSuffixLast
Returns the substring of the specified string from (and including) the last occurrence of the specified character to the end of the string.- Parameters:
str- the string from which a trailing substring is to be extracted.ch- the character whose last occurrence denotes the inclusive start of the suffix.- Returns:
- the substring of
strfrom (and including) the last occurrence ofchto the end of the string. Ifstrdoes not containch,stris returned.
-
getSuffixAfterLast
Returns the substring of the specified string from (but not including) the last occurrence of the specified character to the end of the string.- Parameters:
str- the string from which a trailing substring is to be extracted.ch- the character whose last occurrence denotes the exclusive start of the suffix.- Returns:
- the substring of
strfrom (but not including) the last occurrence ofchto the end of the string. Ifstrdoes not containch,stris returned.
-
removePrefix
Removes the specified prefix (ie, leading substring) from the specified string, if the string starts with the prefix, and returns the resulting string.- Parameters:
str- the string from which to removeprefix.prefix- the leading substring that is to be removed fromstr.- Returns:
strwithoutprefix, ifstrstarts withprefix; otherwise,str.
-
removeSuffix
Removes the specified suffix (ie, trailing substring) from the specified string, if the string ends with the suffix, and returns the resulting string.- Parameters:
str- the string from which to removesuffix.suffix- the trailing substring that is to be removed fromstr.- Returns:
strwithoutsuffix, ifstrends withsuffix; otherwise,str.
-
join
Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences.- Parameters:
separator- the character that will separate adjacent elements ofseqsin the output string.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements.
-
join
Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences.- Parameters:
separator- the character that will separate adjacent elements ofseqsin the output string.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements.
-
join
Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences.- Parameters:
separator- the character sequence that will separate adjacent elements ofseqsin the output string. If it isnull, the elements ofseqswill be concatenated without a separator.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements.
-
join
Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences.- Parameters:
separator- the character sequence that will separate adjacent elements ofseqsin the output string. If it isnull, the elements ofseqswill be concatenated without a separator.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements.
-
join
Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences and an optional separator after the last character sequence.- Parameters:
separator- the character that will separate adjacent elements ofseqsin the output string.includeTrailingSeparator- iftrue, aseparatorwill be appended to the last element ofseqs.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements and an optionalseparatorafter the last element.
-
join
public static String join(char separator, boolean includeTrailingSeparator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified separator between adjacent character sequences and an optional separator after the last character sequence.- Parameters:
separator- the character that will separate adjacent elements ofseqsin the output string.includeTrailingSeparator- iftrue, aseparatorwill be appended to the last element ofseqs.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements and an optionalseparatorafter the last element.
-
join
public static String join(CharSequence separator, boolean includeTrailingSeparator, CharSequence... seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences and an optional separator after the last character sequence.- Parameters:
separator- the character sequence that will separate adjacent elements ofseqsin the output string. If it isnull, the elements ofseqswill be concatenated without a separator.includeTrailingSeparator- iftrue, aseparatorwill be appended to the last element ofseqs.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements and an optionalseparatorafter the last element.
-
join
public static String join(CharSequence separator, boolean includeTrailingSeparator, Iterable<? extends CharSequence> seqs) Returns a string that consists of the specified character sequences in order, with the specified optional separator between adjacent character sequences and an optional separator after the last character sequence.- Parameters:
separator- the character sequence that will separate adjacent elements ofseqsin the output string. If it isnull, the elements ofseqswill be concatenated without a separator.includeTrailingSeparator- iftrue, aseparatorwill be appended to the last element ofseqs.seqs- the character sequences to be joined together.- Returns:
- a string that consists of the elements of
seqs, withseparatorbetween adjacent elements and an optionalseparatorafter the last element.
-
extractLines
Splits the specified text at line separators and returns the resulting list of lines. The line separators that are recognised are CR+LF (U+000D, U+000A) and LF (U+000A). The line separators are not included in the output list.- Parameters:
text- the text that will be split into lines at line separators.- Returns:
- a list of the strings that result from splitting
textat line separators.
-
wrap
Returns the string that results from splitting the specified character sequence into substrings at word breaks that maximise the length of each substring (with the word break removed) while not exceeding the specified maximum length, then joining adjacent substrings with a line feed (U+000A). A word break is a sequence of one or more space characters (U+0020).The length of a substring may exceed the specified limit if the substring does not contain a space character.
- Parameters:
seq- the character sequence that is to be split into substrings at appropriate word breaks.maxLineLength- the maximum length of a substring when splitting the input sequence.- Returns:
- the string that results from splitting
seqinto substrings at word breaks that maximise the length of each substring while not exceedingmaxLineLength, then joining adjacent substrings with a line feed (U+000A).
-
wrapLines
Returns the list of strings that results from splitting the specified character sequence into substrings at word breaks that maximise the length of each substring (with the word break removed) while not exceeding the specified maximum length. A word break is a sequence of one or more space characters (U+0020).The length of a substring may exceed the specified limit if the substring does not contain a space character.
- Parameters:
seq- the character sequence that is to be split into substrings at appropriate word breaks.maxLineLength- the maximum length of a substring when splitting the input sequence.- Returns:
- the list of strings that results from splitting
seqinto substrings at word breaks that maximise the length of each substring while not exceedingmaxLineLength.
-
firstCharToLowerCase
Returns a string that is derived from the specified string by converting its first character to lower case.- Parameters:
str- the string to be converted.- Returns:
strwith its first character converted to lower case.- Throws:
NullPointerException- ifstrisnull.IndexOutOfBoundsException- ifstris empty.
-
firstCharToUpperCase
Returns a string that is derived from the specified string by converting its first character to upper case.- Parameters:
str- the string to be converted.- Returns:
strwith its first character converted to upper case.- Throws:
NullPointerException- ifstrisnull.IndexOutOfBoundsException- ifstris empty.
-
toCamelCase
Converts the specified text to camel case and returns the resulting string.The conversion is described below. In the description,
- a digit is a character for which
Character.isDigit(char)returnstrue, - a letter is a character for which
Character.isLetter(char)returnstrue, - a non-alphanumeric character is a character that is neither a digit nor a letter.
- Each letter that immediately follows a digit or a non-alphanumeric character is converted to upper case except for a letter that follows a sequence of a letter and an apostrophe (U+0027).
- All other letters are converted to lower case.
- All non-alphanumeric characters are removed.
- Parameters:
text- the text that is to be converted.- Returns:
- the camel-case string that results from the conversion of
text.
- a digit is a character for which
-
applyPrefix
Returns a string that is derived from the specified string by applying the specified prefix in a way that depends on whether or not the prefix ends with an underscore ('_', U+005F):- If the prefix ends with an underscore, the returned string consists of the prefix followed by the input string.
- If the prefix does not end with an underscore, the returned string consists of the prefix followed by the input string with its first character converted to upper case.
- Parameters:
str- the string to whichprefixwill be applied.prefix- the prefix that will be applied tostr.- Returns:
- the result of applying
prefixtostrin the way described above.
-
escape
Returns a string that consists of the specified character sequence with each occurrence of any of the specified set of metacharacters immediately preceded by a backslash ('\', U+005C).- Example:
-
If the input sequence is
"${name}"and the metacharacters are"${}", the returned value is"\$\{name\}".
- Parameters:
seq- the string to be processed.metachars- a string containing the metacharacters that will be preceded by a backslash in the output string.- Returns:
- a string consisting of
seqwith each occurrence of any ofmetacharspreceded by a backslash.
-
containsIgnoreCase
Returnstrueif the specified collection of strings contains the specified target string, ignoring letter case when comparing strings.- Parameters:
target- the target string.strs- the strings that will be searched fortarget.- Returns:
trueifstrscontainstarget, ignoring letter case.
-
indexOfIgnoreCase
Returns the index of the first occurrence of the specified target string in the specified collection of strings, ignoring letter case when comparing strings.- Parameters:
target- the target string.strs- the strings that will be searched fortarget.- Returns:
- the index of the first occurrence of
targetinstrs, or -1 ifstrsdoes not containtarget.
-