public class StringUtils extends Object
Simple utility class for String operations useful across the framework.
Some methods in this class were copied from the Spring Framework so we didn't have to re-invent the wheel, and in these cases, we have retained all license, copyright and author information.
| Modifier and Type | Field and Description |
|---|---|
static char |
DEFAULT_DELIMITER_CHAR
Constant representing the default delimiter character (comma), equal to
','
|
static char |
DEFAULT_QUOTE_CHAR
Constant representing the default quote character (double quote), equal to '"'
|
static String |
EMPTY_STRING
Constant representing the empty string, equal to ""
|
| Constructor and Description |
|---|
StringUtils()
|
| Modifier and Type | Method and Description |
|---|---|
static String |
clean(String
Returns a 'cleaned' representation of the specified argument.
|
static boolean |
hasLength(String
Check that the given String is neither
null nor of length 0.
|
static boolean |
hasText(String
Check whether the given String has actual text.
|
static String |
join(Iterator
Joins the elements of the provided
Iterator into a single String containing the provided elements.
|
static String |
split(String
|
static String |
split(String
|
static String |
split(String
|
static String |
split(String
|
static String |
split(String
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves won't be tokenized.
|
static String |
splitKeyValue(String
|
static Set |
splitToSet(String
Splits the
delimited string (delimited by the specified
separator character) and returns the delimited values as a
Set.
|
static boolean |
startsWithIgnoreCase(String
Test if the given String starts with the specified prefix, ignoring upper/lower case.
|
static String |
toDelimitedString(Collection
Returns the collection's contents as a string, with each element delimited by the specified
delimiter argument.
|
static String |
toDelimitedString(Object
Returns the array's contents as a string, with each element delimited by the specified
delimiter argument.
|
static String |
tokenizeToStringArray(String
Tokenize the given String into a String array via a StringTokenizer.
|
static String |
tokenizeToStringArray(String
Tokenize the given String into a String array via a StringTokenizer.
|
static String |
toString(Object
Returns the specified array as a comma-delimited (',') string.
|
static String |
toStringArray(Collection
Copy the given Collection into a String array.
|
static String |
uppercaseFirstChar(String
Returns the input argument, but ensures the first character is capitalized (if possible).
|
public static final StringEMPTY_STRING
public static final char DEFAULT_DELIMITER_CHAR
','
public static final char DEFAULT_QUOTE_CHAR
public static boolean hasText(Stringstr)
true if the string 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
Copied from the Spring Framework while retaining all license, copyright and author information.
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 only
Character.isWhitespace(char)
public static boolean hasLength(Stringstr)
null nor of length 0. Note: Will return
true for a String that purely consists of whitespace.
StringUtils.hasLength(null) == false
StringUtils.hasLength("") == false
StringUtils.hasLength(" ") == true
StringUtils.hasLength("Hello") == true
Copied from the Spring Framework while retaining all license, copyright and author information.
str - the String to check (may be
null)
true if the String is not null and has length
hasText(String)
public static boolean startsWithIgnoreCase(Stringstr, String prefix)
Copied from the Spring Framework while retaining all license, copyright and author information.
str - the String to check
prefix - the prefix to look for
true starts with the specified prefix (ignoring case),
false if it does not.
String.startsWith(java.lang.String, int)
public static Stringclean(String in)
String is null, return nullnull, trim() it.nullnull is returned.
in - the input String to clean.
null otherwise
public static StringtoString(Object [] array)
array - the array whose contents will be converted to a string.
public static StringtoDelimitedString(Object [] array, String delimiter)
delimiter argument. Useful for
toString() implementations and log messages.
array - the array whose contents will be converted to a string
delimiter - the delimiter to use between each element
delimiter.
public static StringtoDelimitedString(Collection c, String delimiter)
delimiter argument. Useful for
toString() implementations and log messages.
c - the collection whose contents will be converted to a string
delimiter - the delimiter to use between each element
delimiter.
public static String[] tokenizeToStringArray(String str, String delimiters)
The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray
Copied from the Spring Framework while retaining all license, copyright and author information.
str - the String to tokenize
delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).
StringTokenizer,
String.trim()
public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray
Copied from the Spring Framework while retaining all license, copyright and author information.
str - the String to tokenize
delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
trimTokens - trim the tokens via String's
trim
ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
null if the input String was
null)
StringTokenizer,
String.trim()
public static String[] toStringArray(Collection collection)
Copied from the Spring Framework while retaining all license, copyright and author information.
collection - the Collection to copy
null if the passed-in Collection was
null)
public static String[] splitKeyValue(String aLine) throws ParseException
ParseException
public static String[] split(String line, char delimiter, char beginQuoteChar, char endQuoteChar)
public static String[] split(String aLine, char delimiter, char beginQuoteChar, char endQuoteChar, boolean retainQuotes, boolean trimTokens)
aLine - the String to parse
delimiter - the delimiter by which the
line argument is to be split
beginQuoteChar - the character signifying the start of quoted text (so the quoted text will not be split)
endQuoteChar - the character signifying the end of quoted text
retainQuotes - if the quotes themselves should be retained when constructing the corresponding token
trimTokens - if leading and trailing whitespace should be trimmed from discovered tokens.
public static Stringjoin(Iterator <?> iterator, String separator)
Iterator into a single String containing the provided elements.
No delimiter is added before or after the list. A
null separator is the same as an empty String ("").
Copied from Commons Lang, version 3 (r1138702).
iterator - the
Iterator of values to join together, may be null
separator - the separator character to use, null treated as ""
null if null iterator input
public static Set<String > splitToSet(String delimited, String separator)
delimited string (delimited by the specified
separator character) and returns the delimited values as a
Set.
If either argument is
null, this method returns
null.
delimited - the string to split
separator - the character that delineates individual tokens to split
Set.
public static StringuppercaseFirstChar(String in)
in - the string to uppercase the first character.