public final class StringUtils extends Object
| 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 ""
|
| Modifier and Type | Method and Description |
|---|---|
static String |
clean(String in)
Returns a 'cleaned' representation of the specified argument.
|
static int |
countOccurrences(String haystack,
char needle)
Count the number of occurrences of a char in the String.
|
static boolean |
hasLength(String str)
Check that the given String is neither
null nor of length 0. |
static boolean |
hasText(String str)
Check whether the given String has actual text.
|
static boolean |
isEmpty(char[] value)
Check whether the given char array has actual text.
|
static boolean |
isEmpty(String value)
Check whether the given String has actual text.
|
static String[] |
split(String aLine)
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves
won't be tokenized.
|
static String[] |
split(String aLine,
char delimiter)
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves
won't be tokenized.
|
static String[] |
split(String aLine,
char delimiter,
char quoteChar)
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves
won't be tokenized.
|
static String[] |
split(String aLine,
char delimiter,
char beginQuoteChar,
char endQuoteChar)
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves
won't be tokenized.
|
static String[] |
split(String aLine,
char delimiter,
char beginQuoteChar,
char endQuoteChar,
boolean retainQuotes,
boolean trimTokens)
Splits the specified delimited String into tokens, supporting quoted tokens so that quoted strings themselves
won't be tokenized.
|
static boolean |
startsWithIgnoreCase(String str,
String prefix)
Test if the given String starts with the specified prefix,
ignoring upper/lower case.
|
static String |
toDelimitedString(Object[] array)
Returns the specified array as a comma-delimited (',') string.
|
static String |
toDelimitedString(String delimiter,
Collection<?> collection)
Returns the collection's contents as a string, with each element delimited by the specified
delimiter argument. |
static String |
toDelimitedString(String separator,
Iterator<?> iterator)
Joins the elements of the provided
Iterator into
a single String containing the provided elements. |
static String |
toDelimitedString(String delimiter,
Object... array)
Returns the array's contents as a string, with each element delimited by the specified
delimiter argument. |
static String[] |
tokenizeToStringArray(String str,
String delimiters)
Tokenize the given String into a String array via a StringTokenizer.
|
static String[] |
tokenizeToStringArray(String str,
String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens)
Tokenize the given String into a String array via a StringTokenizer.
|
static String[] |
toStringArray(Collection<String> collection)
Copy the given Collection into a String array.
|
public static final String EMPTY_STRING
public static final char DEFAULT_DELIMITER_CHAR
','public static final char DEFAULT_QUOTE_CHAR
public static boolean hasText(String str)
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
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 onlyCharacter.isWhitespace(char)public static boolean isEmpty(String value)
true if the string is null or
its length is equal to 0, and it contains no non-whitespace character.
StringUtils.hasText(null) == true
StringUtils.hasText("") == true
StringUtils.hasText(" ") == true
StringUtils.hasText("12345") == false
StringUtils.hasText(" 12345 ") == falsevalue - the String to check (may be null)true if the String is null or its length is
equal to 0, or it does contain whitespace onlyCharacter.isWhitespace(char)public static boolean isEmpty(char[] value)
true if the array is null or
its length is equal to 0, and it contains no non-whitespace character.
value - the String to check (may be null)true if the Array is null or its length is
equal to 0, or it does contain whitespace onlyCharacter.isWhitespace(char)public static boolean hasLength(String str)
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
str - the String to check (may be null)true if the String is not null and has lengthhasText(String)public static boolean startsWithIgnoreCase(String str, String prefix)
str - the String to checkprefix - the prefix to look fortrue starts with the specified prefix (ignoring case), false if it does not.String.startsWith(java.lang.String, int)public static String clean(String in)
String is null, return nullnull, trim() it.nullnull
is returned.in - the input String to clean.null otherwisepublic static String toDelimitedString(Object[] array)
array - the array whose contents will be converted to a string.public static String toDelimitedString(String delimiter, Object... array)
delimiter argument. Useful for toString() implementations and log messages.delimiter - the delimiter to use between each elementarray - the array whose contents will be converted to a stringdelimiter.public static String toDelimitedString(String delimiter, Collection<?> collection)
delimiter argument. Useful for toString() implementations and log messages.delimiter - the delimiter to use between each elementcollection - the collection whose contents will be converted to a stringdelimiter.public static String toDelimitedString(String separator, Iterator<?> iterator)
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 nullseparator - the separator character to use, null treated as ""null if null iterator inputpublic 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;
str - the String to tokenizedelimiters - 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;
str - the String to tokenizedelimiters - the delimiter characters, assembled as String
(each of those characters is individually considered as delimiter)trimTokens - trim the tokens via String's trimignoreEmptyTokens - 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<String> collection)
collection - the Collection to copynull if the passed-in
Collection was null)public static String[] split(String aLine)
aLine - the String to parsepublic static String[] split(String aLine, char delimiter)
aLine - the String to parsedelimiter - the delimiter by which the line argument is to be splitpublic static String[] split(String aLine, char delimiter, char quoteChar)
aLine - the String to parsedelimiter - the delimiter by which the line argument is to be splitquoteChar - the character signifying the quoted text (start and end) (so the quoted text will not be split)public static String[] split(String aLine, char delimiter, char beginQuoteChar, char endQuoteChar)
aLine - the String to parsedelimiter - the delimiter by which the line argument is to be splitbeginQuoteChar - the character signifying the start of quoted text (so the quoted text will not be split)endQuoteChar - the character signifying the end of quoted textpublic static String[] split(String aLine, char delimiter, char beginQuoteChar, char endQuoteChar, boolean retainQuotes, boolean trimTokens)
aLine - the String to parsedelimiter - the delimiter by which the line argument is to be splitbeginQuoteChar - the character signifying the start of quoted text (so the quoted text will not be split)endQuoteChar - the character signifying the end of quoted textretainQuotes - if the quotes themselves should be retained when constructing the corresponding tokentrimTokens - if leading and trailing whitespace should be trimmed from discovered tokens.public static int countOccurrences(String haystack, char needle)
haystack - The String we like to searchneedle - the character to search for.Copyright © 2014–2018. All rights reserved.