public class StringUtils extends Object
String
that are null
safe.
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 | Method and Description |
---|---|
static String |
abbreviate(String str,
String abbrevMarker,
int maxWidth)
Abbreviates a String using ellipses.
|
static String |
capitalize(String str)
Capitalizes a String changing the first character to title case as per
Character.toTitleCase(int) . |
static boolean |
isAllLowerCase(String cs)
Checks if the CharSequence contains only lowercase characters.
|
static boolean |
isAllUpperCase(String cs)
Checks if the CharSequence contains only uppercase characters.
|
static boolean |
isAlpha(String cs)
Checks if the CharSequence contains only Unicode letters.
|
static boolean |
isAlphanumeric(String cs)
Checks if the CharSequence contains only Unicode letters or digits.
|
static boolean |
isNumeric(String cs)
Checks if the CharSequence contains only Unicode digits.
|
static String |
join(Object[] array,
char separator)
Joins the elements of the provided array into a single String containing the provided list of
elements.
|
static int |
length(CharSequence cs)
Gets a CharSequence length or
0 if the CharSequence is null . |
static String |
repeat(String str,
int repeat)
Repeat a String
repeat times to form a new String. |
static String[] |
splitByCharacterTypeCamelCase(String str)
Splits a String by Character type as returned by
java.lang.Character.getType(char) . |
public static String[] splitByCharacterTypeCamelCase(String str)
java.lang.Character.getType(char)
.
Groups of contiguous characters of the same type are returned as complete tokens, with the
following exception: if camelCase
is true
, the character of type Character.UPPERCASE_LETTER
, if any, immediately preceding a token of type Character.LOWERCASE_LETTER
will belong to the following token rather than to the preceding, if
any, Character.UPPERCASE_LETTER
token.str
- the String to split, may be null
null
if null String inputpublic static String join(Object[] array, char separator)
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 String repeat(String str, int repeat)
repeat
times to form a new String.
StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""
str
- the String to repeat, may be nullrepeat
- number of times to repeat str, negative treated as zeronull
if null String
inputpublic static int length(CharSequence cs)
0
if the CharSequence is null
.cs
- a CharSequence or null
0
if the CharSequence is null
.public static String capitalize(String str)
Character.toTitleCase(int)
. No other characters are changed.
A null
input String returns null
.
StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" StringUtils.capitalize("'cat'") = "'cat'"
str
- the String to capitalize, may be nullnull
if null String inputpublic static boolean isAlpha(String cs)
null
will return false
. An empty CharSequence (length()=0) will return
false
.
StringUtils.isAlpha(null) = false StringUtils.isAlpha("") = false StringUtils.isAlpha(" ") = false StringUtils.isAlpha("abc") = true StringUtils.isAlpha("ab2c") = false StringUtils.isAlpha("ab-c") = false
cs
- the CharSequence to check, may be nulltrue
if only contains letters, and is non-nullpublic static boolean isAlphanumeric(String cs)
null
will return false
. An empty CharSequence (length()=0) will return
false
.
StringUtils.isAlphanumeric(null) = false StringUtils.isAlphanumeric("") = false StringUtils.isAlphanumeric(" ") = false StringUtils.isAlphanumeric("abc") = true StringUtils.isAlphanumeric("ab c") = false StringUtils.isAlphanumeric("ab2c") = true StringUtils.isAlphanumeric("ab-c") = false
cs
- the CharSequence to check, may be nulltrue
if only contains letters or digits, and is non-nullpublic static boolean isNumeric(String cs)
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 boolean isAllLowerCase(String cs)
null
will return false
. An empty CharSequence (length()=0) will return
false
.
StringUtils.isAllLowerCase(null) = false StringUtils.isAllLowerCase("") = false StringUtils.isAllLowerCase(" ") = false StringUtils.isAllLowerCase("abc") = true StringUtils.isAllLowerCase("abC") = false StringUtils.isAllLowerCase("ab c") = false StringUtils.isAllLowerCase("ab1c") = false StringUtils.isAllLowerCase("ab/c") = false
cs
- the CharSequence to check, may be nulltrue
if only contains lowercase characters, and is non-nullpublic static boolean isAllUpperCase(String cs)
null
will return false
. An empty String (length()=0) will return false
.
StringUtils.isAllUpperCase(null) = false StringUtils.isAllUpperCase("") = false StringUtils.isAllUpperCase(" ") = false StringUtils.isAllUpperCase("ABC") = true StringUtils.isAllUpperCase("aBC") = false StringUtils.isAllUpperCase("A C") = false StringUtils.isAllUpperCase("A1C") = false StringUtils.isAllUpperCase("A/C") = false
cs
- the CharSequence to check, may be nulltrue
if only contains uppercase characters, and is non-nullpublic static String abbreviate(String str, String abbrevMarker, int maxWidth)
Specifically:
str
is less than or equal to maxWidth
,
return str
.
(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 nullabbrevMarker
- the String indicate abbreviationmaxWidth
- maximum length of result String, must be at least 4null
if null String inputIllegalArgumentException
- if the width is too smallCopyright © 2020. All rights reserved.