public class StringUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
StringUtils.TraditionalBinaryPrefix
The traditional binary prefixes, kilo, mega, ..., exa,
which can be represented by a 64-bit integer.
|
| Modifier and Type | Field and Description |
|---|---|
static char |
COMMA |
static String[] |
emptyStringArray |
static char |
ESCAPE_CHAR |
| Constructor and Description |
|---|
StringUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
arrayToString(String[] strs)
Given an array of strings, return a comma-separated list of its elements.
|
static String |
byteDesc(long len) |
static String |
byteToHexString(byte b)
Convert a byte to a hex string.
|
static String |
byteToHexString(byte[] bytes)
Same as byteToHexString(bytes, 0, bytes.length).
|
static String |
byteToHexString(byte[] bytes,
int start,
int end)
Given an array of bytes it will convert the bytes to a hex string
representation of the bytes
|
static String |
camelize(String s)
Convert SOME_STUFF to SomeStuff
|
static boolean |
equalsIgnoreCase(String s1,
String s2)
Compare strings locale-freely by using String#equalsIgnoreCase.
|
static String |
escapeHTML(String string)
Escapes HTML Special characters present in the string.
|
static String |
escapeString(String str)
Escape commas in the string using the default escape char
|
static String |
escapeString(String str,
char escapeChar,
char charToEscape)
Escape
charToEscape in the string
with the escape char escapeChar |
static String |
escapeString(String str,
char escapeChar,
char[] charsToEscape) |
static int |
findNext(String str,
char separator,
char escapeChar,
int start,
StringBuilder split)
Finds the first occurrence of the separator character ignoring the escaped
separators starting from the index.
|
static String |
format(String format,
Object... objects)
The same as String.format(Locale.ENGLISH, format, objects).
|
static String |
formatPercent(double fraction,
int decimalPlaces)
Format a percentage for presentation to the user.
|
static String |
formatTime(long timeDiff)
Given the time in long milliseconds, returns a
String in the format Xhrs, Ymins, Z sec.
|
static String |
formatTimeDiff(long finishTime,
long startTime)
Given a finish and start time in long milliseconds, returns a
String in the format Xhrs, Ymins, Z sec, for the time difference between two times.
|
static String |
formatTimeSortable(long timeDiff)
Given the time in long milliseconds, returns a String in the sortable
format Xhrs, Ymins, Zsec.
|
static String |
getFormattedTimeWithDiff(String formattedFinishTime,
long finishTime,
long startTime)
Formats time in ms and appends difference (finishTime - startTime)
as returned by formatTimeDiff().
|
static String |
getStackTrace(Thread t)
Get stack trace for a given thread.
|
static Collection<String> |
getStringCollection(String str)
Returns a collection of strings.
|
static Collection<String> |
getStringCollection(String str,
String delim)
Returns a collection of strings.
|
static String[] |
getStrings(String str)
Returns an arraylist of strings.
|
static String[] |
getStrings(String str,
String delim)
Returns an arraylist of strings.
|
static Collection<String> |
getTrimmedStringCollection(String str)
Splits a comma separated value
String, trimming leading and
trailing whitespace on each value. |
static String[] |
getTrimmedStrings(String str)
Splits a comma or newline separated value
String, trimming
leading and trailing whitespace on each value. |
static byte[] |
hexStringToByte(String hex)
Given a hexstring this will return the byte array corresponding to the
string
|
static String |
humanReadableInt(long number)
Deprecated.
|
static boolean |
isAlpha(String str)
Checks if the String contains only unicode letters.
|
static String |
join(char separator,
Iterable<?> strings)
Concatenates strings, using whitespaces as separator.
|
static String |
join(CharSequence separator,
Iterable<?> strings)
Concatenates strings, using a separator.
|
static String |
join(CharSequence separator,
String[] strings)
Concatenates strings, using a separator.
|
static String |
join(char separator,
String[] strings) |
static String |
join(Iterable<String> strings)
Concatenates strings, using whitespaces as separator.
|
static String |
limitDecimalTo2(double d)
Deprecated.
use StringUtils.format("%.2f", d).
|
static String |
replaceTokens(String template,
Pattern pattern,
Map<String,String> replacements)
Matches a template string against a pattern, replaces matched tokens with
the supplied replacements, and returns the result.
|
static String |
simpleHostname(String fullHostname)
Given a full hostname, return the word upto the first dot.
|
static String[] |
split(String str)
Split a string using the default separator
|
static String[] |
split(String str,
char separator)
Split a string using the given separator, with no escaping performed.
|
static String[] |
split(String str,
char escapeChar,
char separator)
Split a string using the given separator
|
static String[] |
splitOnCharWithQuoting(String s,
char splitChar,
char quoteChar,
char escapeChar)
This function splits the String s into multiple Strings using the
splitChar.
|
static String |
stringifyException(Throwable e)
Make a string representation of the exception.
|
static TimeUnit |
stringToTimeUnit(String str) |
static URI[] |
stringToURI(String[] str) |
static String |
timeUnitToString(long time,
TimeUnit unit) |
static String |
toLowerCase(String str)
Converts all of the characters in this String to lower case with
Locale.ENGLISH.
|
static String |
toUpperCase(String str)
Converts all of the characters in this String to upper case with
Locale.ENGLISH.
|
static String |
unEscapeString(String str)
Unescape commas in the string using the default escape char
|
static String |
unEscapeString(String str,
char escapeChar,
char charToEscape)
Unescape
charToEscape in the string
with the escape char escapeChar |
static String |
unEscapeString(String str,
char escapeChar,
char[] charsToEscape) |
static String |
uriToString(URI[] uris) |
public static final String[] emptyStringArray
public static final char COMMA
public static final char ESCAPE_CHAR
public static String stringifyException(Throwable e)
e - The exception to stringifypublic static String simpleHostname(String fullHostname)
fullHostname - the full hostname@Deprecated public static String humanReadableInt(long number)
StringUtils.TraditionalBinaryPrefix.long2String(long, String, int).number - the number to formatpublic static String format(String format, Object... objects)
public static String formatPercent(double fraction, int decimalPlaces)
fraction - the percentage as a fraction, e.g. 0.1 = 10%decimalPlaces - the number of decimal placespublic static String arrayToString(String[] strs)
strs - Array of stringspublic static String byteToHexString(byte[] bytes, int start, int end)
bytes - start - start index, inclusivelyend - end index, exclusivelypublic static String byteToHexString(byte[] bytes)
public static String byteToHexString(byte b)
b - bytebyteToHexString(byte[]),
byteToHexString(byte[], int, int)public static byte[] hexStringToByte(String hex)
hex - the hex String arraypublic static URI[] stringToURI(String[] str)
str - The string array to be parsed into an URI array.IllegalArgumentException - If any string in str violates RFC 2396.public static String formatTimeDiff(long finishTime, long startTime)
finishTime - finish timestartTime - start timepublic static String formatTime(long timeDiff)
timeDiff - The time difference to formatpublic static String formatTimeSortable(long timeDiff)
timeDiff - The time difference to formatpublic static String getFormattedTimeWithDiff(String formattedFinishTime, long finishTime, long startTime)
formattedFinishTime - formattedFinishTime to usefinishTime - finish timestartTime - start timepublic static String[] getStrings(String str)
str - the comma separated string valuespublic static String[] getStrings(String str, String delim)
str - the string valuesdelim - delimiter to separate the valuespublic static Collection<String> getStringCollection(String str)
str - comma separated string valuesArrayList of string valuespublic static Collection<String> getStringCollection(String str, String delim)
str - String to parsedelim - delimiter to separate the valuespublic static Collection<String> getTrimmedStringCollection(String str)
String, trimming leading and
trailing whitespace on each value. Duplicate and empty values are removed.str - a comma separated Collection of String values, empty
Collection if null String inputpublic static String[] getTrimmedStrings(String str)
String, trimming
leading and trailing whitespace on each value.str - a comma or newline separated String with values,
may be nullString values, empty array if null String
inputpublic static String[] split(String str)
str - a string that may have escaped separatorpublic static String[] split(String str, char escapeChar, char separator)
str - a string that may have escaped separatorescapeChar - a char that be used to escape the separatorseparator - a separator charpublic static String[] split(String str, char separator)
str - a string to be split. Note that this may not be null.separator - a separator charpublic static int findNext(String str, char separator, char escapeChar, int start, StringBuilder split)
str - the source stringseparator - the character to findescapeChar - character used to escapestart - from where to searchsplit - used to pass back the extracted stringpublic static String[] splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
s - The String to splitsplitChar - quoteChar - public static String escapeString(String str)
str - a stringpublic static String escapeString(String str, char escapeChar, char charToEscape)
charToEscape in the string
with the escape char escapeCharstr - stringescapeChar - escape charcharToEscape - the char to be escapedpublic static String escapeString(String str, char escapeChar, char[] charsToEscape)
charsToEscape - array of characters to be escapedpublic static String unEscapeString(String str)
str - a stringpublic static String unEscapeString(String str, char escapeChar, char charToEscape)
charToEscape in the string
with the escape char escapeCharstr - stringescapeChar - escape charcharToEscape - the escaped charpublic static String unEscapeString(String str, char escapeChar, char[] charsToEscape)
charsToEscape - array of characters to unescapepublic static String escapeHTML(String string)
string - public static String byteDesc(long len)
@Deprecated public static String limitDecimalTo2(double d)
public static String join(CharSequence separator, Iterable<?> strings)
separator - Separator to join with.strings - Strings to join.public static String join(Iterable<String> strings)
strings - Strings to join.public static String join(char separator, Iterable<?> strings)
strings - Strings to join.public static String join(CharSequence separator, String[] strings)
separator - to join withstrings - to joinpublic static String camelize(String s)
s - input stringpublic static String replaceTokens(String template, Pattern pattern, Map<String,String> replacements)
template - String template to receive replacementspattern - Pattern to match for identifying tokens, must use a capturing
groupreplacements - Mappublic static String toLowerCase(String str)
str - string to be convertedpublic static String toUpperCase(String str)
str - string to be convertedpublic static boolean equalsIgnoreCase(String s1, String s2)
s1 - Non-null string to be converteds2 - string to be convertedpublic static boolean isAlpha(String str)
Checks if the String contains only unicode letters.
null will return false.
An empty String (length()=0) will return true.
StringUtils.isAlpha(null) = false
StringUtils.isAlpha("") = true
StringUtils.isAlpha(" ") = false
StringUtils.isAlpha("abc") = true
StringUtils.isAlpha("ab2c") = false
StringUtils.isAlpha("ab-c") = false
str - the String to check, may be nulltrue if only contains letters, and is non-nullCopyright © 2018. All rights reserved.