public abstract class StringUtils extends Object
This class delivers some simple functionality that should really be provided by the core Java String and StringBuffer classes, such as the ability to replace all occurrences of a given substring in a target string. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.
Modifier and Type | Field and Description |
---|---|
static String |
FOLDER_SEPARATOR |
static String |
FORMAT_TIME_STAMP |
static String |
NEW_LINE |
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
appendProperties(StringBuilder sb,
Properties properties) |
static String |
arrayToCommaDelimitedString(Object[] arr)
Convenience method to return a String array as a CSV String.
|
static String |
arrayToDelimitedString(Object[] arr,
String delim)
Convenience method to return a String array as a delimited (e.g.
|
static String |
collectionToCommaDelimitedString(Collection<String> coll)
Convenience method to return a Collection as a CSV String.
|
static String |
collectionToDelimitedString(Collection<String> coll,
String delim)
Convenience method to return a Collection as a delimited (e.g.
|
static String |
collectionToDelimitedString(Collection<String> coll,
String delim,
String prefix,
String suffix)
Convenience method to return a Collection as a delimited (e.g.
|
static String[] |
commaDelimitedListToStringArray(String str)
Convert a CSV list into an array of Strings.
|
static Map<String,String> |
convertArrayToKeyValuePairs(String[] pairs,
String keyValueSeperator) |
static Set<String> |
convertArrayToSet(String[] array)
Transforms array of String objects to Set
|
static String[] |
convertKeyValuePairsToArray(Map<String,String> value,
String keyValueSeperator) |
static int |
countOccurrencesOf(String s,
char c)
Count the occurrences of the character c in string s.
|
static String[] |
delimitedListToStringArray(String str,
String delimiter)
Take a String which is a delimited list and convert it to a String array.
|
static boolean |
endsWithIgnoreCase(String str,
String suffix)
Test if the given String ends with the specified suffix, ignoring upper/lower case.
|
static boolean |
equalsIgnoreCase(String str1,
String str2) |
static String |
getCurrentStackTrace() |
static Set<String> |
getParametersSet(String locatorsStr)
Transforms string with elements separated by "," to Set of elements
|
static String |
getSuffix(String s,
String separator) |
static String |
getTimeStamp() |
static String |
getTimeStamp(long timeInMillis) |
static boolean |
hasLength(String str)
Check if a String has length.
|
static boolean |
hasText(String str)
Check if a String has text.
|
static int |
indexOfIgnoreCase(String str1,
String str2) |
static boolean |
isEmpty(String str) |
static boolean |
isStrictPrefix(String s,
String prefix) |
static String |
join(Collection<String> collection,
String separator) |
static String |
join(String[] array,
String delimiter,
int firstIndex,
int count) |
static boolean |
startsWithIgnoreCase(String str,
String prefix)
Test if the given String starts with the specified prefix, ignoring upper/lower case.
|
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 List<String> |
toList(String s,
String delimiter) |
static Properties |
toProperties(String s,
String propertyDelimiter,
String keyValueSeparator) |
static String[] |
toStringArray(Collection<String> collection)
Copy the given Collection into a String array.
|
static String |
trimToNull(String s) |
public static final String FOLDER_SEPARATOR
public static final String NEW_LINE
public static String FORMAT_TIME_STAMP
public static boolean isEmpty(String str)
public static boolean hasLength(String str)
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 lengthpublic static boolean hasText(String str)
true
if the string not
null, it's length is > 0
, and it has at least one non-whitespace
character.
StringUtils.hasText(null) = false
StringUtils.hasText("") = false
StringUtils.hasText(" ") = false
StringUtils.hasText("12345") = true
StringUtils.hasText(" 12345 ") = true
- Parameters:
str
- the String to check, may be null
- Returns:
true
if the String is not null, length > 0, and not whitespace only
- See Also:
Character.isWhitespace(char)
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(String str,
String prefix)
Test if the given String starts with the specified prefix, ignoring upper/lower case.
- Parameters:
str
- the String to check
prefix
- the prefix to look for
- See Also:
String.startsWith(java.lang.String, int)
-
endsWithIgnoreCase
public static boolean endsWithIgnoreCase(String str,
String suffix)
Test if the given String ends with the specified suffix, ignoring upper/lower case.
- Parameters:
str
- the String to check
suffix
- the suffix to look for
- See Also:
String.endsWith(java.lang.String)
-
countOccurrencesOf
public static int countOccurrencesOf(String s,
char c)
Count the occurrences of the character c in string s.
- Parameters:
s
- String to scan.
c
- Character to find and count.
- Returns:
- Number of occurrences of c in s.
-
toStringArray
public static String[] toStringArray(Collection<String> collection)
Copy the given Collection into a String array. The Collection must contain String elements
only.
- Parameters:
collection
- the Collection to copy
- Returns:
- the String array (
null
if the Collection was null
as well)
-
join
public static String join(Collection<String> collection,
String separator)
-
toProperties
public static Properties toProperties(String s,
String propertyDelimiter,
String keyValueSeparator)
-
tokenizeToStringArray
public static String[] tokenizeToStringArray(String str,
String delimiters)
Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits
empty tokens. 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
- Parameters:
str
- the String to tokenize
delimiters
- the delimiter characters, assembled as String (each of those characters is
individually considered as delimiter).
- Returns:
- an array of the tokens
- See Also:
StringTokenizer
,
String.trim()
,
delimitedListToStringArray(java.lang.String, java.lang.String)
-
tokenizeToStringArray
public static String[] tokenizeToStringArray(String str,
String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens)
Tokenize the given String into a String array via a StringTokenizer. 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
- Parameters:
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).
- Returns:
- an array of the tokens
- See Also:
StringTokenizer
,
String.trim()
,
delimitedListToStringArray(java.lang.String, java.lang.String)
-
delimitedListToStringArray
public static String[] delimitedListToStringArray(String str,
String delimiter)
Take a String which is a delimited list and convert it to a String array. A single
delimiter can consists of more than one character: It will still be considered as single
delimiter string, rather than as bunch of potential delimiter characters - in contrast to
tokenizeToStringArray
.
- Parameters:
str
- the input String
delimiter
- the delimiter between elements (this is a single delimiter, rather than a
bunch individual delimiter characters)
- Returns:
- an array of the tokens in the list
- See Also:
tokenizeToStringArray(java.lang.String, java.lang.String)
-
commaDelimitedListToStringArray
public static String[] commaDelimitedListToStringArray(String str)
Convert a CSV list into an array of Strings.
- Parameters:
str
- CSV list
- Returns:
- an array of Strings, or the empty array if s is null
-
arrayToDelimitedString
public static String arrayToDelimitedString(Object[] arr,
String delim)
Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for
toString() implementations.
- Parameters:
arr
- array to display. Elements may be of any type (toString will be called on each
element).
delim
- delimiter to use (probably a ",")
-
collectionToDelimitedString
public static String collectionToDelimitedString(Collection<String> coll,
String delim,
String prefix,
String suffix)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for
toString() implementations.
- Parameters:
coll
- Collection to display
delim
- delimiter to use (probably a ",")
prefix
- string to start each element with
suffix
- string to end each element with
-
collectionToDelimitedString
public static String collectionToDelimitedString(Collection<String> coll,
String delim)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for
toString() implementations.
- Parameters:
coll
- Collection to display
delim
- delimiter to use (probably a ",")
-
arrayToCommaDelimitedString
public static String arrayToCommaDelimitedString(Object[] arr)
Convenience method to return a String array as a CSV String. E.g. useful for toString()
implementations.
- Parameters:
arr
- array to display. Elements may be of any type (toString will be called on each
element).
-
collectionToCommaDelimitedString
public static String collectionToCommaDelimitedString(Collection<String> coll)
Convenience method to return a Collection as a CSV String. E.g. useful for toString()
implementations.
- Parameters:
coll
- Collection to display
-
convertArrayToSet
public static Set<String> convertArrayToSet(String[] array)
Transforms array of String objects to Set
- Parameters:
array
- array of String instances
- Returns:
- set
-
getParametersSet
public static Set<String> getParametersSet(String locatorsStr)
Transforms string with elements separated by "," to Set of elements
- Parameters:
locatorsStr
- elements delimetered by ","
- Returns:
- set
-
appendProperties
public static void appendProperties(StringBuilder sb,
Properties properties)
-
convertKeyValuePairsToArray
public static String[] convertKeyValuePairsToArray(Map<String,String> value,
String keyValueSeperator)
-
convertArrayToKeyValuePairs
public static Map<String,String> convertArrayToKeyValuePairs(String[] pairs,
String keyValueSeperator)
-
getTimeStamp
public static String getTimeStamp()
-
getTimeStamp
public static String getTimeStamp(long timeInMillis)
-
getCurrentStackTrace
public static String getCurrentStackTrace()
Copyright © GigaSpaces.