public final class StringUtils
extends java.lang.Object
Operations on String that are null safe.
The StringUtils class defines certain words related to String
handling.
null"")' ', char 32)Character.isWhitespace(char)String.trim()
StringUtils handles null input Strings quietly.
That is to say that a null input will return null.
Where a boolean or int is being returned details
vary by method.
A side effect of the null handling is that a
NullPointerException should be considered a bug in
StringUtils (except for deprecated methods).
Methods in this class give sample code to explain their operation. The symbol
* is used to indicate any input including null.
String| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
EMPTY
The empty String
"". |
static int |
INDEX_NOT_FOUND
Represents a failed index search.
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
abbreviate(java.lang.String str,
int maxWidth)
Abbreviates a String using ellipses.
|
static java.lang.String |
abbreviate(java.lang.String str,
int offset,
int maxWidth)
Abbreviates a String using ellipses.
|
static boolean |
containsAny(java.lang.String str,
char... searchChars)
Checks if the String contains any character in the given
set of characters.
|
static int |
countMatches(java.lang.String str,
java.lang.String sub)
Counts how many times the substring appears in the larger String.
|
static java.lang.String |
defaultIfBlank(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
whitespace, empty ("") or
null, the value of defaultStr. |
static java.lang.String |
defaultIfEmpty(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in String, or if the String is
empty or
null, the value of defaultStr. |
static <T> T |
defaultIfNull(T object,
T defaultValue)
Returns a default value if the object passed is
null. |
static java.lang.String |
defaultString(java.lang.String str)
Returns either the passed in String, or if the String is
null, an empty String (""). |
static java.lang.String |
defaultString(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in String, or if the String is
null, the value of defaultStr. |
static boolean |
equals(java.lang.Object object1,
java.lang.Object object2)
Compares two objects for equality, where either one or both
objects may be
null. |
static boolean |
isBlank(java.lang.String str)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isEmpty(java.lang.String str)
Checks if a String is empty ("") or null.
|
static java.lang.String |
join(java.lang.Object[] array,
char separator)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static java.lang.String |
join(java.lang.Object[] array,
char separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static java.lang.String |
join(java.lang.Object[] array,
java.lang.String separator)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static java.lang.String |
join(java.lang.Object[] array,
java.lang.String separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static <T> java.lang.String |
join(T... elements)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static java.lang.String |
leftPad(java.lang.String str,
int size)
Left pad a String with spaces (' ').
|
static java.lang.String |
leftPad(java.lang.String str,
int size,
char padChar)
Left pad a String with a specified character.
|
static java.lang.String |
leftPad(java.lang.String str,
int size,
java.lang.String padStr)
Left pad a String with a specified String.
|
static java.lang.String |
replace(java.lang.String text,
java.lang.String searchString,
java.lang.String replacement)
Replaces all occurrences of a String within another String.
|
static java.lang.String |
replace(java.lang.String text,
java.lang.String searchString,
java.lang.String replacement,
int max)
Replaces a String with another String inside a larger String,
for the first
max values of the search String. |
static java.lang.String |
replaceEach(java.lang.String text,
java.lang.String[] searchList,
java.lang.String[] replacementList)
Replaces all occurrences of Strings within another String.
|
static java.lang.String |
rightPad(java.lang.String str,
int size)
Right pad a String with spaces (' ').
|
static java.lang.String |
rightPad(java.lang.String str,
int size,
char padChar)
Right pad a String with a specified character.
|
static java.lang.String |
rightPad(java.lang.String str,
int size,
java.lang.String padStr)
Right pad a String with a specified String.
|
static java.lang.String[] |
split(java.lang.String regex,
java.lang.CharSequence input)
A custom adaptation of
Pattern.split(CharSequence, int). |
static java.lang.String |
toCamelCase(java.lang.String string)
Convert a string to camel case
|
static java.lang.String |
toCamelCaseLC(java.lang.String string)
Convert a string to camel case starting with a lower case letter
|
static java.lang.String |
toLC(java.lang.String string)
Change a string's first letter to lower case
|
public static final java.lang.String EMPTY
"".public static final int INDEX_NOT_FOUND
public static java.lang.String defaultString(java.lang.String str)
Returns either the passed in String, or if the String is
null, an empty String ("").
StringUtils.defaultString(null) = ""
StringUtils.defaultString("") = ""
StringUtils.defaultString("bat") = "bat"
str - the String to check, may be nullnullString.valueOf(Object)public static java.lang.String defaultString(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in String, or if the String is
null, the value of defaultStr.
StringUtils.defaultString(null, "NULL") = "NULL"
StringUtils.defaultString("", "NULL") = ""
StringUtils.defaultString("bat", "NULL") = "bat"
str - the String to check, may be nulldefaultStr - the default String to return
if the input is null, may be nullnullString.valueOf(Object)public static java.lang.String defaultIfEmpty(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in String, or if the String is
empty or null, the value of defaultStr.
StringUtils.defaultIfEmpty(null, "NULL") = "NULL"
StringUtils.defaultIfEmpty("", "NULL") = "NULL"
StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
str - the String to check, may be nulldefaultStr - the default String to return
if the input is empty ("") or null, may be nulldefaultString(String, String)public static java.lang.String defaultIfBlank(java.lang.String str,
java.lang.String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
whitespace, empty ("") or null, the value of defaultStr.
StringUtils.defaultIfBlank(null, "NULL") = "NULL"
StringUtils.defaultIfBlank("", "NULL") = "NULL"
StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
StringUtils.defaultIfBlank("bat", "NULL") = "bat"
StringUtils.defaultIfBlank("", null) = null
str - the CharSequence to check, may be nulldefaultStr - the default CharSequence to return
if the input is whitespace, empty ("") or null, may be nulldefaultString(String, String)public static boolean isEmpty(java.lang.String str)
Checks if a String is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
str - the String to check, may be nulltrue if the String is empty or nullpublic static boolean isBlank(java.lang.String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
str - the String to check, may be nulltrue if the String is null, empty or whitespacepublic static int countMatches(java.lang.String str,
java.lang.String sub)
Counts how many times the substring appears in the larger String.
A null or empty ("") String input returns 0.
StringUtils.countMatches(null, *) = 0
StringUtils.countMatches("", *) = 0
StringUtils.countMatches("abba", null) = 0
StringUtils.countMatches("abba", "") = 0
StringUtils.countMatches("abba", "a") = 2
StringUtils.countMatches("abba", "ab") = 1
StringUtils.countMatches("abba", "xxx") = 0
str - the String to check, may be nullsub - the substring to count, may be nullnullpublic static java.lang.String rightPad(java.lang.String str,
int size)
Right pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.rightPad(null, *) = null
StringUtils.rightPad("", 3) = " "
StringUtils.rightPad("bat", 3) = "bat"
StringUtils.rightPad("bat", 5) = "bat "
StringUtils.rightPad("bat", 1) = "bat"
StringUtils.rightPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String inputpublic static java.lang.String rightPad(java.lang.String str,
int size,
char padChar)
Right pad a String with a specified character.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, 'z') = "zzz"
StringUtils.rightPad("bat", 3, 'z') = "bat"
StringUtils.rightPad("bat", 5, 'z') = "batzz"
StringUtils.rightPad("bat", 1, 'z') = "bat"
StringUtils.rightPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String inputpublic static java.lang.String rightPad(java.lang.String str,
int size,
java.lang.String padStr)
Right pad a String with a specified String.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, "z") = "zzz"
StringUtils.rightPad("bat", 3, "yz") = "bat"
StringUtils.rightPad("bat", 5, "yz") = "batyz"
StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
StringUtils.rightPad("bat", 1, "yz") = "bat"
StringUtils.rightPad("bat", -1, "yz") = "bat"
StringUtils.rightPad("bat", 5, null) = "bat "
StringUtils.rightPad("bat", 5, "") = "bat "
str - the String to pad out, may be nullsize - the size to pad topadStr - the String to pad with, null or empty treated as single spacenull if null String inputpublic static java.lang.String leftPad(java.lang.String str,
int size)
Left pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.leftPad(null, *) = null
StringUtils.leftPad("", 3) = " "
StringUtils.leftPad("bat", 3) = "bat"
StringUtils.leftPad("bat", 5) = " bat"
StringUtils.leftPad("bat", 1) = "bat"
StringUtils.leftPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String inputpublic static java.lang.String leftPad(java.lang.String str,
int size,
char padChar)
Left pad a String with a specified character.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, 'z') = "zzz"
StringUtils.leftPad("bat", 3, 'z') = "bat"
StringUtils.leftPad("bat", 5, 'z') = "zzbat"
StringUtils.leftPad("bat", 1, 'z') = "bat"
StringUtils.leftPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String inputpublic static java.lang.String leftPad(java.lang.String str,
int size,
java.lang.String padStr)
Left pad a String with a specified String.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, "z") = "zzz"
StringUtils.leftPad("bat", 3, "yz") = "bat"
StringUtils.leftPad("bat", 5, "yz") = "yzbat"
StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
StringUtils.leftPad("bat", 1, "yz") = "bat"
StringUtils.leftPad("bat", -1, "yz") = "bat"
StringUtils.leftPad("bat", 5, null) = " bat"
StringUtils.leftPad("bat", 5, "") = " bat"
str - the String to pad out, may be nullsize - the size to pad topadStr - the String to pad with, null or empty treated as single spacenull if null String inputpublic static java.lang.String abbreviate(java.lang.String str,
int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."
Specifically:
str is less than maxWidth characters
long, return it.(substring(str, 0, max-3) + "...").maxWidth is less than 4, throw an
IllegalArgumentException.maxWidth.
StringUtils.abbreviate(null, *) = null
StringUtils.abbreviate("", 4) = ""
StringUtils.abbreviate("abcdefg", 6) = "abc..."
StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
StringUtils.abbreviate("abcdefg", 4) = "a..."
StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException
str - the String to check, may be nullmaxWidth - maximum length of result String, must be at least 4null if null String inputjava.lang.IllegalArgumentException - if the width is too smallpublic static java.lang.String abbreviate(java.lang.String str,
int offset,
int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "...is the time for..."
Works like abbreviate(String, int), but allows you to specify
a "left edge" offset. Note that this left edge is not necessarily going to
be the leftmost character in the result, or the first character following the
ellipses, but it will appear somewhere in the result.
In no case will it return a String of length greater than
maxWidth.
StringUtils.abbreviate(null, *, *) = null
StringUtils.abbreviate("", 0, 4) = ""
StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..."
StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..."
StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..."
StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno"
StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException
StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException
str - the String to check, may be nulloffset - left edge of source StringmaxWidth - maximum length of result String, must be at least 4null if null String inputjava.lang.IllegalArgumentException - if the width is too smallpublic static boolean containsAny(java.lang.String str,
char... searchChars)
Checks if the String contains any character in the given set of characters.
A null String will return false.
A null or zero length search array will return false.
StringUtils.containsAny(null, *) = false
StringUtils.containsAny("", *) = false
StringUtils.containsAny(*, null) = false
StringUtils.containsAny(*, []) = false
StringUtils.containsAny("zzabyycdxx",['z','a']) = true
StringUtils.containsAny("zzabyycdxx",['b','y']) = true
StringUtils.containsAny("aba", ['z']) = false
str - the String to check, may be nullsearchChars - the chars to search for, may be nulltrue if any of the chars are found,
false if no match or null inputpublic static java.lang.String replace(java.lang.String text,
java.lang.String searchString,
java.lang.String replacement)
Replaces all occurrences of a String within another String.
A null reference passed to this method is a no-op.
StringUtils.replace(null, *, *) = null
StringUtils.replace("", *, *) = ""
StringUtils.replace("any", null, *) = "any"
StringUtils.replace("any", *, null) = "any"
StringUtils.replace("any", "", *) = "any"
StringUtils.replace("aba", "a", null) = "aba"
StringUtils.replace("aba", "a", "") = "b"
StringUtils.replace("aba", "a", "z") = "zbz"
text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace it with, may be nullnull if null String inputreplace(String text, String searchString, String replacement, int max)public static java.lang.String replace(java.lang.String text,
java.lang.String searchString,
java.lang.String replacement,
int max)
Replaces a String with another String inside a larger String,
for the first max values of the search String.
A null reference passed to this method is a no-op.
StringUtils.replace(null, *, *, *) = null
StringUtils.replace("", *, *, *) = ""
StringUtils.replace("any", null, *, *) = "any"
StringUtils.replace("any", *, null, *) = "any"
StringUtils.replace("any", "", *, *) = "any"
StringUtils.replace("any", *, *, 0) = "any"
StringUtils.replace("abaa", "a", null, -1) = "abaa"
StringUtils.replace("abaa", "a", "", -1) = "b"
StringUtils.replace("abaa", "a", "z", 0) = "abaa"
StringUtils.replace("abaa", "a", "z", 1) = "zbaa"
StringUtils.replace("abaa", "a", "z", 2) = "zbza"
StringUtils.replace("abaa", "a", "z", -1) = "zbzz"
text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace it with, may be nullmax - maximum number of values to replace, or -1 if no maximumnull if null String inputpublic static java.lang.String replaceEach(java.lang.String text,
java.lang.String[] searchList,
java.lang.String[] replacementList)
Replaces all occurrences of Strings within another String.
A null reference passed to this method is a no-op, or if
any "search string" or "string to replace" is null, that replace will be
ignored. This will not repeat. For repeating replaces, call the
overloaded method.
StringUtils.replaceEach(null, *, *) = null
StringUtils.replaceEach("", *, *) = ""
StringUtils.replaceEach("aba", null, null) = "aba"
StringUtils.replaceEach("aba", new String[0], null) = "aba"
StringUtils.replaceEach("aba", null, new String[0]) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, null) = "aba"
StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}) = "b"
StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}) = "aba"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
(example of how it does not repeat)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "dcte"
text - text to search and replace in, no-op if nullsearchList - the Strings to search for, no-op if nullreplacementList - the Strings to replace them with, no-op if nullnull if
null String inputjava.lang.IndexOutOfBoundsException - if the lengths of the arrays are not the same (null is ok,
and/or size 0)@SafeVarargs public static <T> java.lang.String join(T... elements)
Joins the elements of the provided array into a single String containing the provided list of elements.
No separator is added to the joined String. Null objects or empty strings within the array are represented by empty strings.
StringUtils.join(null) = null StringUtils.join([]) = "" StringUtils.join([null]) = "" StringUtils.join(["a", "b", "c"]) = "abc" StringUtils.join([null, "", "a"]) = "a"
T - the specific type of values to join togetherelements - the values to join together, may be nullnull if null array inputpublic static java.lang.String join(java.lang.Object[] array,
char separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], ';') = "a;b;c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join([null, "", "a"], ';') = ";;a"
array - the array of values to join together, may be nullseparator - the separator character to usenull if null array inputpublic static java.lang.String join(java.lang.Object[] array,
char separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], ';') = "a;b;c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join([null, "", "a"], ';') = ";;a"
array - the array of values to join together, may be nullseparator - the separator character to usestartIndex - the first index to start joining from. It is
an error to pass in an end index past the end of the arrayendIndex - the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the arraynull if null array inputpublic static java.lang.String join(java.lang.Object[] array,
java.lang.String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list.
A null separator is the same as an empty String ("").
Null objects or empty strings within the array are represented by
empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = ",,a"
array - the array of values to join together, may be nullseparator - the separator character to use, null treated as ""null if null array inputpublic static java.lang.String join(java.lang.Object[] array,
java.lang.String separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list.
A null separator is the same as an empty String ("").
Null objects or empty strings within the array are represented by
empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = ",,a"
array - the array of values to join together, may be nullseparator - the separator character to use, null treated as ""startIndex - the first index to start joining from. It is
an error to pass in an end index past the end of the arrayendIndex - the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the arraynull if null array inputpublic static boolean equals(java.lang.Object object1,
java.lang.Object object2)
Compares two objects for equality, where either one or both
objects may be null.
ObjectUtils.equals(null, null) = true
ObjectUtils.equals(null, "") = false
ObjectUtils.equals("", null) = false
ObjectUtils.equals("", "") = true
ObjectUtils.equals(Boolean.TRUE, null) = false
ObjectUtils.equals(Boolean.TRUE, "true") = false
ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
object1 - the first object, may be nullobject2 - the second object, may be nulltrue if the values of both objects are the samepublic static <T> T defaultIfNull(T object,
T defaultValue)
Returns a default value if the object passed is null.
ObjectUtils.defaultIfNull(null, null) = null
ObjectUtils.defaultIfNull(null, "") = ""
ObjectUtils.defaultIfNull(null, "zz") = "zz"
ObjectUtils.defaultIfNull("abc", *) = "abc"
ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
T - the type of the objectobject - the Object to test, may be nulldefaultValue - the default value to return, may be nullobject if it is not null, defaultValue otherwisepublic static java.lang.String toCamelCase(java.lang.String string)
public static java.lang.String toCamelCaseLC(java.lang.String string)
public static java.lang.String toLC(java.lang.String string)
public static java.lang.String[] split(java.lang.String regex,
java.lang.CharSequence input)
Pattern.split(CharSequence, int).
This is useful if the matched split-tokens should be returned as well.
For example:
split("e", "hello world") // ["h", "e", "llo world"]
split("o", "hello world") // ["hell", "o", " w", "o", "rld"]
split("[eo]", "hello world") // ["h", "e", "ll", "o", " w", "o", "rld"]
The result will always be an odd-length array.
Copyright © 2018. All Rights Reserved.