public class StringUtils
extends java.lang.Object
String or CharSequence instances.| Modifier and Type | Field | Description |
|---|---|---|
static java.lang.String |
EMPTY |
The empty
String |
| Constructor | Description |
|---|---|
StringUtils() |
| Modifier and Type | Method | Description |
|---|---|---|
static java.lang.String |
arrayToDelimitedString(java.lang.Object[] arr,
java.lang.String delim) |
Convert a
String array into a delimited String (e.g. |
static boolean |
containsWhitespace(java.lang.CharSequence str) |
Check whether the given
CharSequence contains any whitespace characters. |
static boolean |
containsWhitespace(java.lang.String str) |
Check whether the given
String contains any whitespace characters. |
static java.lang.String |
convertToHumanFriendlyByteSize(long size) |
Convert byte size into human friendly format.
|
static long |
convertToMachineFriendlyByteSize(java.lang.String size) |
Convert byte size into machine friendly format.
|
static java.lang.String |
emptyToNull(java.lang.String str) |
Returns the given string if it is nonempty;
null otherwise. |
static boolean |
endsWith(java.lang.String str,
char suffix) |
Test if the given
String ends with the specified prefix character. |
static boolean |
endsWithIgnoreCase(java.lang.String str,
java.lang.String suffix) |
Test if the given
String ends with the specified suffix,
ignoring upper/lower case. |
static boolean |
hasLength(java.lang.CharSequence str) |
Check that the given
CharSequence is neither null nor
of length 0. |
static boolean |
hasLength(java.lang.String str) |
Check that the given
String is neither null nor of length 0. |
static boolean |
hasText(java.lang.CharSequence str) |
Check whether the given
CharSequence contains actual text. |
static boolean |
hasText(java.lang.String str) |
Check whether the given
String contains actual text. |
static boolean |
isEmpty(java.lang.String str) |
Returns
true if the given string is null or is the empty string. |
static java.lang.String |
joinCommaDelimitedList(java.lang.String[] arr) |
Convert a
String array into a comma delimited String
(i.e., CSV). |
static java.lang.String |
joinCommaDelimitedList(java.util.Collection<?> list) |
Convert a
Collection into a comma delimited String
(i.e., CSV). |
static java.lang.String |
nullToEmpty(java.lang.String str) |
Returns the given string if it is non-null; the empty string otherwise.
|
static java.util.Locale |
parseLocaleString(java.lang.String localeString) |
Parse the given
localeString value into a Locale. |
static java.util.TimeZone |
parseTimeZoneString(java.lang.String timeZoneString) |
Parse the given
timeZoneString value into a TimeZone. |
static java.lang.String |
replace(java.lang.String str,
java.lang.String[] search,
java.lang.String[] replace) |
Replace all occurrences of a substring within a string with another string.
|
static java.lang.String |
replace(java.lang.String str,
java.lang.String search,
java.lang.String replace) |
Replace all occurrences of a substring within a string with another string.
|
static int |
search(java.lang.CharSequence chars,
char c) |
Returns the number of times the specified character was found
in the target string, or 0 if there is no specified character.
|
static int |
search(java.lang.String str,
java.lang.String keyw) |
Returns the number of times the specified string was found
in the target string, or 0 if there is no specified string.
|
static int |
searchIgnoreCase(java.lang.CharSequence chars,
char c) |
Returns the number of times the specified character was found
in the target string, or 0 if there is no specified character.
|
static int |
searchIgnoreCase(java.lang.String str,
java.lang.String keyw) |
Returns the number of times the specified string was found
in the target string, or 0 if there is no specified string.
|
static java.lang.String[] |
split(java.lang.String str,
char delim) |
Returns an array of strings separated by the delimiter string.
|
static java.lang.String[] |
split(java.lang.String str,
char delim,
int size) |
Returns an array of strings separated by the delimiter string.
|
static java.lang.String[] |
split(java.lang.String str,
java.lang.String delim) |
Returns an array of strings separated by the delimiter string.
|
static java.lang.String[] |
split(java.lang.String str,
java.lang.String delim,
int size) |
Returns an array of strings separated by the delimiter string.
|
static java.lang.String[] |
splitCommaDelimitedString(java.lang.String str) |
Convert a comma delimited list (e.g., a row from a CSV file) into an
array of strings.
|
static boolean |
startsWith(java.lang.String str,
char prefix) |
Test if the given
String starts with the specified prefix character. |
static boolean |
startsWithIgnoreCase(java.lang.String str,
java.lang.String prefix) |
Test if the given
String starts with the specified prefix,
ignoring upper/lower case. |
static java.lang.String[] |
tokenize(java.lang.String str,
java.lang.String delimiters) |
Tokenize the given
String into a String array via a StringTokenizer. |
static java.lang.String[] |
tokenize(java.lang.String str,
java.lang.String delimiters,
boolean trim) |
Tokenize the given
String into a String array via a StringTokenizer. |
static java.lang.String |
toLanguageTag(java.util.Locale locale) |
Determine the RFC 3066 compliant language tag,
as used for the HTTP "Accept-Language" header.
|
static java.lang.String |
trimAllWhitespace(java.lang.String str) |
Trim all whitespace from the given
String:
leading, trailing, and in between characters. |
static java.lang.String |
trimLeadingCharacter(java.lang.String str,
char leadingCharacter) |
Trim all occurrences of the supplied leading character from the given
String. |
static java.lang.String |
trimLeadingWhitespace(java.lang.String str) |
Trim leading whitespace from the given
String. |
static java.lang.String |
trimTrailingCharacter(java.lang.String str,
char trailingCharacter) |
Trim all occurrences of the supplied trailing character from the given
String. |
static java.lang.String |
trimTrailingWhitespace(java.lang.String str) |
Trim trailing whitespace from the given
String. |
static java.lang.String |
trimWhitespace(java.lang.String str) |
Trim leading and trailing whitespace from the given
String. |
public static final java.lang.String EMPTY
Stringpublic static boolean hasLength(java.lang.CharSequence str)
CharSequence is neither null nor
of length 0.
Note: this method returns true for a CharSequence
that purely consists of whitespace.
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true
str - the CharSequence to check (may be null)true if the CharSequence is not null and has lengthhasText(String)public static boolean hasLength(java.lang.String str)
String is neither null nor of length 0.
Note: this method returns true for a String that
purely consists of whitespace.
str - the String to check (may be null)true if the String is not null and has lengthhasLength(CharSequence),
hasText(String)public static boolean hasText(java.lang.CharSequence str)
CharSequence contains actual text.
More specifically, this method returns true if the
CharSequence is not null, its length is greater than
0, and it contains at least one non-whitespace character.
StringUtils.hasText(null) = false
StringUtils.hasText("") = false
StringUtils.hasText(" ") = false
StringUtils.hasText("12345") = true
StringUtils.hasText(" 12345 ") = true
str - the CharSequence to check (may be null)true if the CharSequence is not null,
its length is greater than 0, and it does not contain whitespace onlyCharacter.isWhitespace(char)public static boolean hasText(java.lang.String str)
String contains actual text.
More specifically, this method returns true if the
String is not null, its length is greater than 0,
and it contains at least one non-whitespace character.
str - the String to check (may be null)true if the String is not null, its
length is greater than 0, and it does not contain whitespace onlyhasText(CharSequence)public static boolean containsWhitespace(java.lang.CharSequence str)
CharSequence contains any whitespace characters.str - the CharSequence to check (may be null)true if the CharSequence is not empty and
contains at least 1 whitespace characterCharacter.isWhitespace(char)public static boolean containsWhitespace(java.lang.String str)
String contains any whitespace characters.str - the String to check (may be null)true if the String is not empty and
contains at least 1 whitespace charactercontainsWhitespace(CharSequence)public static java.lang.String trimWhitespace(java.lang.String str)
String.str - the String to checkStringCharacter.isWhitespace(char)public static java.lang.String trimAllWhitespace(java.lang.String str)
String:
leading, trailing, and in between characters.str - the String to checkStringCharacter.isWhitespace(char)public static java.lang.String trimLeadingWhitespace(java.lang.String str)
String.str - the String to checkStringCharacter.isWhitespace(char)public static java.lang.String trimTrailingWhitespace(java.lang.String str)
String.str - the String to checkStringCharacter.isWhitespace(char)public static java.lang.String trimLeadingCharacter(java.lang.String str,
char leadingCharacter)
String.str - the String to checkleadingCharacter - the leading character to be trimmedStringpublic static java.lang.String trimTrailingCharacter(java.lang.String str,
char trailingCharacter)
String.str - the String to checktrailingCharacter - the trailing character to be trimmedStringpublic static boolean startsWithIgnoreCase(java.lang.String str,
java.lang.String prefix)
String starts with the specified prefix,
ignoring upper/lower case.str - the String to checkprefix - the prefix to look fortrue if the String starts with the prefix, case insensitive, or both nullString.startsWith(java.lang.String, int)public static boolean endsWithIgnoreCase(java.lang.String str,
java.lang.String suffix)
String ends with the specified suffix,
ignoring upper/lower case.str - the String to checksuffix - the suffix to look fortrue if the String ends with the suffix, case insensitive, or both nullString.endsWith(java.lang.String)public static boolean isEmpty(java.lang.String str)
true if the given string is null or is the empty string.str - a string reference to checktrue if the string is null or is the empty stringpublic static java.lang.String nullToEmpty(java.lang.String str)
str - the string to test and possibly returnstring itself if it is non-null; "" if it is nullpublic static java.lang.String emptyToNull(java.lang.String str)
null otherwise.str - the string to test and possibly returnstring itself if it is nonempty; null if it is empty or nullpublic static boolean startsWith(java.lang.String str,
char prefix)
String starts with the specified prefix character.str - the String to checkprefix - the prefix character to look forString.startsWith(java.lang.String, int)public static boolean endsWith(java.lang.String str,
char suffix)
String ends with the specified prefix character.str - the String to checksuffix - the prefix character to look forString.endsWith(java.lang.String)public static java.lang.String replace(java.lang.String str,
java.lang.String search,
java.lang.String replace)
str - String to examinesearch - String to replacereplace - String to insertString with the replacementspublic static java.lang.String replace(java.lang.String str,
java.lang.String[] search,
java.lang.String[] replace)
str - String to examinesearch - String array to replacereplace - String array to insertString with the replacementspublic static java.lang.String[] split(java.lang.String str,
java.lang.String delim)
str - the string to be separateddelim - the delimiterpublic static java.lang.String[] split(java.lang.String str,
java.lang.String delim,
int size)
str - the string to be separateddelim - the delimitersize - the size of the arraypublic static java.lang.String[] split(java.lang.String str,
char delim)
str - the string to be separateddelim - the delimiterpublic static java.lang.String[] split(java.lang.String str,
char delim,
int size)
str - the string to be separateddelim - the delimitersize - the size of the arraypublic static int search(java.lang.String str,
java.lang.String keyw)
str - the target stringkeyw - the string to findpublic static int searchIgnoreCase(java.lang.String str,
java.lang.String keyw)
str - the target stringkeyw - the string to findpublic static int search(java.lang.CharSequence chars,
char c)
chars - the target stringc - the character to findpublic static int searchIgnoreCase(java.lang.CharSequence chars,
char c)
chars - the target stringc - the character to findpublic static java.lang.String[] tokenize(java.lang.String str,
java.lang.String delimiters)
String into a String array via a StringTokenizer.str - the String to tokenizedelimiters - the delimiter characterspublic static java.lang.String[] tokenize(java.lang.String str,
java.lang.String delimiters,
boolean trim)
String into a String array via a StringTokenizer.str - the String to tokenizedelimiters - the delimiter characterstrim - trim the tokens via String's trimpublic static java.lang.String arrayToDelimitedString(java.lang.Object[] arr,
java.lang.String delim)
String array into a delimited String (e.g. CSV).
Useful for toString() implementations.
arr - the array to displaydelim - the delimiter to use (typically a ",")Stringpublic static java.lang.String[] splitCommaDelimitedString(java.lang.String str)
str - the input Stringpublic static java.lang.String joinCommaDelimitedList(java.lang.String[] arr)
String array into a comma delimited String
(i.e., CSV).arr - the array to displayStringpublic static java.lang.String joinCommaDelimitedList(java.util.Collection<?> list)
Collection into a comma delimited String
(i.e., CSV).list - the collectionStringpublic static java.util.Locale parseLocaleString(java.lang.String localeString)
localeString value into a Locale.
This is the inverse operation of Locale's toString.
localeString - the locale String, following Locale's
toString() format ("en", "en_UK", etc);
also accepts spaces as separators, as an alternative to underscoresLocale instancejava.lang.IllegalArgumentException - in case of an invalid locale specificationpublic static java.lang.String toLanguageTag(java.util.Locale locale)
locale - the Locale to transform to a language tagStringpublic static java.util.TimeZone parseTimeZoneString(java.lang.String timeZoneString)
timeZoneString value into a TimeZone.timeZoneString - the time zone String, following TimeZone.getTimeZone(String)
but throwing IllegalArgumentException in case of an invalid time zone specificationTimeZone instancejava.lang.IllegalArgumentException - in case of an invalid time zone specificationpublic static java.lang.String convertToHumanFriendlyByteSize(long size)
size - the number of bytespublic static long convertToMachineFriendlyByteSize(java.lang.String size)
size - the human friendly byte size (includes units)java.lang.NumberFormatException - if failed parse given sizeCopyright © 2008–2018 The Aspectran Project. All rights reserved.