Operations on java. 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.
java.lang.String
| Modifier and Type | Field and Description |
|---|---|
| public static final String | EMPTY
The empty String |
| public static final int | INDEX_NOT_FOUND
Represents a failed index search. |
| private static final int | PAD_LIMIT
The maximum size to which the padding constant(s) can expand. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static String | Returns: abbreviated String,null if null String inputthe String to check, may be null str, int maximum length of result String, must be at least 4 maxWidth)Abbreviates a String using ellipses. |
| public static String | Returns: abbreviated String,null if null String inputthe String to check, may be null str, int left edge of source String offset, int maximum length of result String, must be at least 4 maxWidth)Abbreviates a String using ellipses. |
| public static boolean | Returns: thetrue if any of the chars are found,
false if no match or null inputthe String to check, may be null str, char... the chars to search for, may be null searchChars)Checks if the String contains any character in the given set of characters. |
| public static int | Returns: the number of occurrences, 0 if either String isnullthe String to check, may be null str, String the substring to count, may be null sub)Counts how many times the substring appears in the larger String. |
| public static String | Returns: the passed in CharSequence, or the defaultthe CharSequence to check, may be null str, String the default CharSequence to return
if the input is whitespace, empty ("") or defaultStr)null, may be nullReturns either the passed in CharSequence, or if the CharSequence is
whitespace, empty ("") or |
| public static String | Returns: the passed in String, or the defaultthe String to check, may be null str, String the default String to return
if the input is empty ("") or defaultStr)null, may be nullReturns either the passed in String, or if the String is
empty or |
| public static < the type of the object T> T | Returns: object if it is not null, defaultValue otherwisethe object, T Object to test, may be nullthe default value to return, may be defaultValue)nullReturns a default value if the object passed is |
| public static String | Returns: the passed in String, or the empty String if it wasnullthe String to check, may be null str)
Returns either the passed in String, or if the String is
|
| public static String | Returns: the passed in String, or the default if it wasnullthe String to check, may be null str, String the default String to return
if the input is defaultStr)null, may be nullReturns either the passed in String, or if the String is
|
| public static boolean | |
| public static boolean | |
| public static < the type of the objects T> T | Returns: first non-null element in objects, otherwise nullthe elements to test, may not be objects)null but emptyReturns the first non- |
| public static boolean | |
| public static boolean | |
| public static < the specific type of values to join together T> String | Returns: the joined String,null if null array inputthe values to join together, may be null elements)Joins the elements of the provided array into a single String containing the provided list of elements. |
| public static String | |
| public static String | Returns: the joined String,null if null array inputthe array of values to join together, may be null array, char the separator character to use separator, int the first index to start joining from. It is
an error to pass in an end index past the end of the array startIndex, int the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the array endIndex)Joins the elements of the provided array into a single String containing the provided list of elements. |
| public static String | |
| public static String | Returns: the joined String,null if null array inputthe array of values to join together, may be null array, String the separator character to use, null treated as "" separator, int the first index to start joining from. It is
an error to pass in an end index past the end of the array startIndex, int the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the array endIndex)Joins the elements of the provided array into a single String containing the provided list of elements. |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| private static String | Returns: String with repeated characternumber of times to repeat delim repeat, char character to repeat padChar)Returns padding using the specified delimiter repeated to a given length. |
| public static String | Returns: the text with any replacements processed,null if null String inputtext to search and replace in, may be null text, String the String to search for, may be null searchString, String the String to replace it with, may be null replacement)Replaces all occurrences of a String within another String. |
| public static String | Returns: the text with any replacements processed,null if null String inputtext to search and replace in, may be null text, String the String to search for, may be null searchString, String the String to replace it with, may be null replacement, int maximum number of values to replace, or max)-1 if no maximumReplaces a String with another String inside a larger String,
for the first |
| public static String | Returns: the text with any replacements processed,null if
null String inputtext to search and replace in, no-op if null text, String[] the Strings to search for, no-op if null searchList, String[] the Strings to replace them with, no-op if null replacementList)Replaces all occurrences of Strings within another String. |
| private static String | Returns: the text with any replacements processed,null if
null String inputtext to search and replace in, no-op if null text, String[] the Strings to search for, no-op if null searchList, String[] the Strings to replace them with, no-op if null replacementList, boolean if true, then replace repeatedly
until there are no more possible replacements or timeToLive < 0 repeat, int if less than 0 then there is a circular reference and endless
loop timeToLive)Replaces all occurrences of Strings within another String. |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| public static String | Returns: right padded String or original String if no padding is necessary,null if null String inputthe String to pad out, may be null str, int the size to pad to size, String the String to pad with, null or empty treated as single space padStr)Right pad a String with a specified String. |
| public static String | |
| public static String[] | |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| public static String | |
| public static String |
| EMPTY | back to summary |
|---|---|
| public static final String EMPTY The empty String
| |
| INDEX_NOT_FOUND | back to summary |
|---|---|
| public static final int INDEX_NOT_FOUND Represents a failed index search.
| |
| PAD_LIMIT | back to summary |
|---|---|
| private static final int PAD_LIMIT The maximum size to which the padding constant(s) can expand. | |
| StringUtils | back to summary |
|---|---|
| private StringUtils() | |
| abbreviate | back to summary |
|---|---|
| public static String abbreviate(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:
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
| |
| abbreviate | back to summary |
|---|---|
| public static String abbreviate(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 In no case will it return a String of length greater than
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
| |
| containsAny | back to summary |
|---|---|
| public static boolean containsAny(String str, char... searchChars) Checks if the String contains any character in the given set of characters. A
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
| |
| countMatches | back to summary |
|---|---|
| public static int countMatches(String str, String sub) Counts how many times the substring appears in the larger String.
A
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
| |
| defaultIfBlank | back to summary |
|---|---|
| public static String defaultIfBlank(String str, String defaultStr) Returns either the passed in CharSequence, or if the CharSequence is
whitespace, empty ("") or
StringUtils.defaultIfBlank(null, "NULL") = "NULL"
StringUtils.defaultIfBlank("", "NULL") = "NULL"
StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
StringUtils.defaultIfBlank("bat", "NULL") = "bat"
StringUtils.defaultIfBlank("", null) = null
| |
| defaultIfEmpty | back to summary |
|---|---|
| public static String defaultIfEmpty(String str, String defaultStr) Returns either the passed in String, or if the String is
empty or
StringUtils.defaultIfEmpty(null, "NULL") = "NULL"
StringUtils.defaultIfEmpty("", "NULL") = "NULL"
StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
| |
| defaultIfNull | back to summary |
|---|---|
| public static <T> T defaultIfNull(T object, T defaultValue) Returns a default value if the object passed is
ObjectUtils.defaultIfNull(null, null) = null
ObjectUtils.defaultIfNull(null, "") = ""
ObjectUtils.defaultIfNull(null, "zz") = "zz"
ObjectUtils.defaultIfNull("abc", *) = "abc"
ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
| |
| defaultString | back to summary |
|---|---|
| public static String defaultString(String str)
Returns either the passed in String, or if the String is
StringUtils.defaultString(null) = ""
StringUtils.defaultString("") = ""
StringUtils.defaultString("bat") = "bat"
| |
| defaultString | back to summary |
|---|---|
| public static String defaultString(String str, String defaultStr) Returns either the passed in String, or if the String is
StringUtils.defaultString(null, "NULL") = "NULL"
StringUtils.defaultString("", "NULL") = ""
StringUtils.defaultString("bat", "NULL") = "bat"
| |
| equals | back to summary |
|---|---|
| public static boolean equals(String o1, String o2)
Compares two strings for equality, where either one or both objects may
be
ObjectUtils.equals(null, null) = true
ObjectUtils.equals(null, "") = false
ObjectUtils.equals("", null) = false
ObjectUtils.equals("", "") = true
| |
| equals | back to summary |
|---|---|
| public static boolean equals(Object o1, Object o2)
Compares two objects for deep equality, where either one or both objects
may be
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
| |
| firstNonNull | back to summary |
|---|---|
| public static <T> T firstNonNull(T... objects) Returns the first non-
| |
| isBlank | back to summary |
|---|---|
| public static boolean isBlank(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
| |
| isEmpty | back to summary |
|---|---|
| public static boolean isEmpty(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().
| |
| join | back to summary |
|---|---|
| public static <T> 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"
| |
| join | back to summary |
|---|---|
| public static String join(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" | |
| join | back to summary |
|---|---|
| public static String join(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"
| |
| join | back to summary |
|---|---|
| public static String join(Object[] array, 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 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" | |
| join | back to summary |
|---|---|
| public static String join(Object[] array, 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 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"
| |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int size) Left pad a String with spaces (' '). The String is padded to the size of
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"
| |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int strSize, int size) | |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int size, char padChar) Left pad a String with a specified character. Pad to a size of
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"
| |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int strSize, int size, char padChar) | |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int size, String padStr) Left pad a String with a specified String. Pad to a size of
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"
| |
| leftPad | back to summary |
|---|---|
| public static String leftPad(String str, int strSize, int size, String padStr) | |
| padding | back to summary |
|---|---|
| private static String padding(int repeat, char padChar) throws IndexOutOfBoundsException Returns padding using the specified delimiter repeated to a given length. StringUtils.padding(0, 'e') = "" StringUtils.padding(3, 'e') = "eee" StringUtils.padding(-2, 'e') = IndexOutOfBoundsException Note this method doesn't not support padding with
Unicode Supplementary Characters
as they require a pair of
| |
| replace | back to summary |
|---|---|
| public static String replace(String text, String searchString, String replacement) Replaces all occurrences of a String within another String. A
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"
| |
| replace | back to summary |
|---|---|
| public static String replace(String text, String searchString, String replacement, int max) Replaces a String with another String inside a larger String,
for the first A
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"
| |
| replaceEach | back to summary |
|---|---|
| public static String replaceEach(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String.
A
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"
| |
| replaceEach | back to summary |
|---|---|
| private static String replaceEach(String text, String[] searchList, String[] replacementList, boolean repeat, int timeToLive) Replaces all occurrences of Strings within another String.
A
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 repeats)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, false) = "dcte"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, true) = "tcte"
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, *) = IllegalArgumentException
| |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int size) Right pad a String with spaces (' '). The String is padded to the size of
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"
| |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int strSize, int size) | |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int size, char padChar) Right pad a String with a specified character. The String is padded to the size of
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"
| |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int strSize, int size, char padChar) | |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int size, String padStr) Right pad a String with a specified String. The String is padded to the size of
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 "
| |
| rightPad | back to summary |
|---|---|
| public static String rightPad(String str, int strSize, int size, String padStr) | |
| split | back to summary |
|---|---|
| public static String[] split(String regex, CharSequence input) A custom adaptation of This is useful if the matched split-tokens should be returned as well. For example:
The result will always be an odd-length array. | |
| toCamelCase | back to summary |
|---|---|
| public static String toCamelCase(String string) Convert a string to camel case | |
| toCamelCaseLC | back to summary |
|---|---|
| public static String toCamelCaseLC(String string) Convert a string to camel case starting with a lower case letter | |
| toLC | back to summary |
|---|---|
| public static String toLC(String string) Change a string's first letter to lower case | |
| toLC | back to summary |
|---|---|
| public static String toLC(String string, Locale locale) Change a string's first letter to lower case | |
| toUC | back to summary |
|---|---|
| public static String toUC(String string) Change a string's first letter to upper case | |
| toUC | back to summary |
|---|---|
| public static String toUC(String string, Locale locale) Change a string's first letter to upper case | |