public class StringUtils extends 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.
Methods in this class give sample code to explain their operation.
The symbol * is used to indicate any input including null.
#ThreadSafe#
String| Modifier and Type | Field and Description |
|---|---|
static String |
EMPTY
The empty String
"". |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsAny(CharSequence cs,
char... searchChars)
Checks if the CharSequence contains any character in the given.
|
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static boolean |
isNumeric(CharSequence cs)
Checks if the CharSequence contains only Unicode digits.
|
static String |
join(Object[] parts,
String separator)
Joins the elements of the provided array into a single String containing the
provided list of elements.
|
public static final String EMPTY
"".public static boolean isEmpty(CharSequence cs)
Checks if a CharSequence 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 CharSequence. That functionality is available in isBlank().
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or nullpublic static boolean containsAny(CharSequence cs, char... searchChars)
Checks if the CharSequence contains any character in the given. set of characters.
A null CharSequence 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
cs - the CharSequence 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 boolean isNumeric(CharSequence cs)
Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
null will return false. An empty CharSequence (length()=0)
will return false.
Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = false
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("१२३") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
StringUtils.isNumeric("-123") = false
StringUtils.isNumeric("+123") = false
cs - the CharSequence to check, may be nulltrue if only contains digits, and is non-nullpublic static String join(Object[] parts, 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], *) = "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"], ',') = "null,,a"
parts - - the array of values to join together, may be nullseparator - - the separator character to use, null treated as ""null if null array inputCopyright © 2022. All rights reserved.