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 void |
resolvePlaceholders(Properties properties) |
static void |
resolvePlaceholders(Properties properties,
Map<String,String> environment) |
static String |
resolvePlaceholders(String text) |
static String |
resolvePlaceholders(String text,
Map<String,String> environment) |
static String |
resolvePlaceholders(String text,
Map<String,String> environment,
String prefix,
String suffix) |
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
str
- the String to check, may be null
true
if the String is not null, length > 0, and not whitespace onlyCharacter.isWhitespace(char)
public static boolean startsWithIgnoreCase(String str, String prefix)
str
- the String to checkprefix
- the prefix to look forString.startsWith(java.lang.String, int)
public static boolean endsWithIgnoreCase(String str, String suffix)
str
- the String to checksuffix
- the suffix to look forString.endsWith(java.lang.String)
public static int countOccurrencesOf(String s, char c)
s
- String to scan.c
- Character to find and count.public static String[] toStringArray(Collection<String> collection)
collection
- the Collection to copynull
if the Collection was null
as well)public static String join(Collection<String> collection, String separator)
public static Properties toProperties(String s, String propertyDelimiter, String keyValueSeparator)
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
str
- the String to tokenizedelimiters
- the delimiter characters, assembled as String (each of those characters is
individually considered as delimiter).StringTokenizer
,
String.trim()
,
delimitedListToStringArray(java.lang.String, java.lang.String)
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
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 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).StringTokenizer
,
String.trim()
,
delimitedListToStringArray(java.lang.String, java.lang.String)
public static String[] delimitedListToStringArray(String str, String delimiter)
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
.
str
- the input Stringdelimiter
- the delimiter between elements (this is a single delimiter, rather than a
bunch individual delimiter characters)tokenizeToStringArray(java.lang.String, java.lang.String)
public static String[] commaDelimitedListToStringArray(String str)
str
- CSV listpublic static String arrayToDelimitedString(Object[] arr, String delim)
arr
- array to display. Elements may be of any type (toString will be called on each
element).delim
- delimiter to use (probably a ",")public static String collectionToDelimitedString(Collection<String> coll, String delim, String prefix, String suffix)
coll
- Collection to displaydelim
- delimiter to use (probably a ",")prefix
- string to start each element withsuffix
- string to end each element withpublic static String collectionToDelimitedString(Collection<String> coll, String delim)
coll
- Collection to displaydelim
- delimiter to use (probably a ",")public static String arrayToCommaDelimitedString(Object[] arr)
arr
- array to display. Elements may be of any type (toString will be called on each
element).public static String collectionToCommaDelimitedString(Collection<String> coll)
coll
- Collection to displaypublic static Set<String> convertArrayToSet(String[] array)
array
- array of String instancespublic static Set<String> getParametersSet(String locatorsStr)
locatorsStr
- elements delimetered by ","public static void appendProperties(StringBuilder sb, Properties properties)
public static String[] convertKeyValuePairsToArray(Map<String,String> value, String keyValueSeperator)
public static Map<String,String> convertArrayToKeyValuePairs(String[] pairs, String keyValueSeperator)
public static String getTimeStamp()
public static String getTimeStamp(long timeInMillis)
public static String getCurrentStackTrace()
public static void resolvePlaceholders(Properties properties)
public static void resolvePlaceholders(Properties properties, Map<String,String> environment)
public static String resolvePlaceholders(String text, Map<String,String> environment)
Copyright © GigaSpaces.