Class StringUtils
Operations on String that are
null safe.
- IsEmpty - checks if a String contains text
- IndexOfAny - index-of any of a set of Strings
The StringUtils class defines certain words related to
String handling.
- null -
null - empty - a zero-length string (
"") - space - the space character (
' ', char 32) - whitespace - the characters defined by
Character.isWhitespace(char) - trim - the characters <= 32 as in
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#
- Since:
- 1.0
- Version:
- $Id: StringUtils.java 1648067 2014-12-27 16:45:42Z britter $
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsAny(CharSequence cs, char... searchChars) Checks if the CharSequence contains any character in the given.static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty ("") or null.static booleanChecks if the CharSequence contains only Unicode digits.static StringJoins the elements of the provided array into a single String containing the provided list of elements.
-
Field Details
-
EMPTY
The empty String"".- Since:
- 2.0
- See Also:
-
-
Method Details
-
isEmpty
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 ") = falseNOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null- Since:
- 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
-
containsAny
Checks if the CharSequence contains any character in the given. set of characters.
A
nullCharSequence will returnfalse. Anullor zero length search array will returnfalse.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- Parameters:
cs- the CharSequence to check, may be nullsearchChars- the chars to search for, may be null- Returns:
- the
trueif any of the chars are found,falseif no match or null input - Since:
- 2.4, 3.0 Changed signature from containsAny(String, char[]) to containsAny(CharSequence, char...)
-
isNumeric
Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.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- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains digits, and is non-null- Since:
- 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence), 3.0 Changed "" to return false and not true
-
join
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
nullseparator 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"
- Parameters:
parts- - the array of values to join together, may be nullseparator- - the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null array input
-