Class StringUtils
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.
- Author:
- kimchy
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendProperties(StringBuilder sb, Properties properties) static StringConvenience method to return a String array as a CSV String.static StringarrayToDelimitedString(Object[] arr, String delim) Convenience method to return a String array as a delimited (e.g.static StringConvenience method to return a Collection as a CSV String.static StringcollectionToDelimitedString(Collection<String> coll, String delim) Convenience method to return a Collection as a delimited (e.g.static StringcollectionToDelimitedString(Collection<String> coll, String delim, String prefix, String suffix) Convenience method to return a Collection as a delimited (e.g.static String[]Convert a CSV list into an array of Strings.convertArrayToKeyValuePairs(String[] pairs, String keyValueSeperator) convertArrayToSet(String[] array) Transforms array of String objects to Setstatic String[]convertKeyValuePairsToArray(Map<String, String> value, String keyValueSeperator) static intcountOccurrencesOf(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 booleanendsWithIgnoreCase(String str, String suffix) Test if the given String ends with the specified suffix, ignoring upper/lower case.static booleanequalsIgnoreCase(String str1, String str2) static Stringstatic Stringstatic StringgetFileName(String path, boolean removeExtension) getParametersSet(String locatorsStr) Transforms string with elements separated by "," to Set of elementsstatic Stringstatic Stringstatic StringgetTimeStamp(long timeInMillis) static booleanCheck if a String has length.static booleanCheck if a String has text.static intindexOfIgnoreCase(String str1, String str2) static booleanstatic booleanisStrictPrefix(String s, String prefix) static Stringstatic Stringjoin(Collection<String> collection, String separator) static Stringstatic LongparseDurationAsMillis(String property) static LongparseStringAsBytes(String property) static TimeUnitstatic TimeUnitparseTimeUnit(String s, TimeUnit defaultValue) static StringresolvePlaceholders(String text) static StringresolvePlaceholders(String text, Map<String, String> environment) static Stringstatic voidresolvePlaceholders(Properties properties) static voidresolvePlaceholders(Properties properties, Map<String, String> environment) static Stringstatic booleanstartsWithIgnoreCase(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 PropertiestoProperties(String s, String propertyDelimiter, String keyValueSeparator) static String[]toStringArray(Collection<String> collection) Copy the given Collection into a String array.static StringtrimToNull(String s)
-
Field Details
-
FOLDER_SEPARATOR
- See Also:
-
NEW_LINE
-
FORMAT_TIME_STAMP
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
isEmpty
-
hasLength
Check if a String has length.StringUtils.hasLength(null) = false StringUtils.hasLength("") = false StringUtils.hasLength(" ") = true StringUtils.hasLength("Hello") = true- Parameters:
str- the String to check, may benull- Returns:
trueif the String is not null and has length
-
hasText
Check if a String has text. More specifically, returnstrueif the string notnull, it'slength 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 benull- Returns:
trueif the String is not null, length > 0, and not whitespace only- See Also:
-
equalsIgnoreCase
-
startsWithIgnoreCase
Test if the given String starts with the specified prefix, ignoring upper/lower case.- Parameters:
str- the String to checkprefix- the prefix to look for- See Also:
-
endsWithIgnoreCase
Test if the given String ends with the specified suffix, ignoring upper/lower case.- Parameters:
str- the String to checksuffix- the suffix to look for- See Also:
-
indexOfIgnoreCase
-
countOccurrencesOf
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
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 (
nullif the Collection wasnullas well)
-
join
-
join
-
toList
-
toProperties
-
tokenizeToStringArray
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 tokenizedelimiters- the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).- Returns:
- an array of the tokens
- See Also:
-
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 tokenizedelimiters- the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)trimTokens- trim the tokens via String'strimignoreEmptyTokens- 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:
-
delimitedListToStringArray
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 Stringdelimiter- 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:
-
commaDelimitedListToStringArray
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
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 displaydelim- delimiter to use (probably a ",")prefix- string to start each element withsuffix- string to end each element with
-
collectionToDelimitedString
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.- Parameters:
coll- Collection to displaydelim- delimiter to use (probably a ",")
-
arrayToCommaDelimitedString
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
Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.- Parameters:
coll- Collection to display
-
convertArrayToSet
Transforms array of String objects to Set- Parameters:
array- array of String instances- Returns:
- set
-
getParametersSet
Transforms string with elements separated by "," to Set of elements- Parameters:
locatorsStr- elements delimetered by ","- Returns:
- set
-
appendProperties
-
convertKeyValuePairsToArray
-
convertArrayToKeyValuePairs
-
getTimeStamp
-
getTimeStamp
-
getSuffix
-
getFileName
-
getCurrentStackTrace
-
isStrictPrefix
-
trimToNull
-
resolvePlaceholders
-
resolvePlaceholders
-
resolvePlaceholders
-
resolvePlaceholders
-
resolvePlaceholders
-
extract
-
parseStringAsBytes
-
parseDurationAsMillis
-
parseTimeUnit
-
parseTimeUnit
-
leftPad
-
rightPad
-