Class StringUtils
String
that are
null
safe.
Largely inspired by Apache Commons Lang3.
Note: This class is for internal use only and subject to backward incompatible change at any time.
- Author:
- brenuart
-
Method Summary
Modifier and TypeMethodDescriptionstatic String[]
Convert a comma delimited list into an array of strings.static String[]
delimitedListToStringArray
(String str, String delimiter) Take aString
that is a delimited list and convert it into aString
array.static boolean
isBlank
(CharSequence cs) Checks if a CharSequence is empty (""), null or whitespace only.static boolean
isEmpty
(CharSequence cs) Checks if a CharSequence is empty ("") or null.static int
length
(CharSequence cs) Gets a CharSequence length or0
if the CharSequence isnull
.static String
Removes control characters (char <= 32) from both ends of this String, handlingnull
by returningnull
.static String
trimToEmpty
(String str) Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it isnull
.static String
trimToNull
(String str) Removes control characters (char <= 32) from both ends of this String returningnull
if the String is empty ("") after the trim or if it isnull
.
-
Method Details
-
trim
Removes control characters (char <= 32) from both ends of this String, handling
null
by returningnull
.The String is trimmed using
String.trim()
. Trim removes start and end characters <= 32.StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"
- Parameters:
str
- the String to be trimmed, may be null- Returns:
- the trimmed string,
null
if null String input
-
trimToEmpty
Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is
null
.The String is trimmed using
String.trim()
. Trim removes start and end characters <= 32.StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"
- Parameters:
str
- the String to be trimmed, may be null- Returns:
- the trimmed String, or an empty String if
null
input
-
trimToNull
Removes control characters (char <= 32) from both ends of this String returning
null
if the String is empty ("") after the trim or if it isnull
.The String is trimmed using
String.trim()
. Trim removes start and end characters <= 32.StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"
- Parameters:
str
- the String to be trimmed, may be null- Returns:
- the trimmed String,
null
if only chars <= 32, empty or null String input
-
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 ") = false
- Parameters:
cs
- the CharSequence to check, may be null- Returns:
true
if the CharSequence is empty or null
-
isBlank
Checks if a CharSequence is empty (""), null or whitespace only.
Whitespace is defined by
Character.isWhitespace(char)
.StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
- Parameters:
cs
- the CharSequence to check, may be null- Returns:
true
if the CharSequence is null, empty or whitespace only
-
length
Gets a CharSequence length or0
if the CharSequence isnull
.- Parameters:
cs
- a CharSequence ornull
- Returns:
- CharSequence length or
0
if the CharSequence isnull
.
-
commaDelimitedListToStringArray
Convert a comma delimited list into an array of strings.- Parameters:
str
- the inputString
(potentiallynull
or empty)- Returns:
- an array of strings, or the empty array in case of empty input
- See Also:
-
delimitedListToStringArray
Take aString
that is a delimited list and convert it into aString
array.A single
delimiter
may consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters. Delimiter can be escaped by prefixing it with a backslash (\
).Values are trimmed, and are added to the resulting array only if not blank. Therefore two consecutive delimiters are treated as a single delimiter.
A
null
delimiter is treated as no delimiter and returns an array with the originalstr
string as single element. An empty delimiter splits the input string at each character.A
null
input returns an empty array.- Parameters:
str
- the inputString
(potentiallynull
or empty)delimiter
- the delimiter between elements- Returns:
- an array of the tokens in the list
-