@Private
@Unstable
public class StringUtils
extends java.lang.Object
| Modifier and Type | Class | 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 | Description |
|---|---|---|
static char |
COMMA |
|
static java.lang.String |
COMMA_STR |
|
static java.lang.String[] |
emptyStringArray |
|
static java.util.regex.Pattern |
ENV_VAR_PATTERN |
Regular expression that matches and captures environment variable names
according to platform-specific rules.
|
static char |
ESCAPE_CHAR |
|
static java.util.regex.Pattern |
SHELL_ENV_VAR_PATTERN |
Shell environment variables: $ followed by one letter or _ followed by
multiple letters, numbers, or underscores.
|
static int |
SHUTDOWN_HOOK_PRIORITY |
Priority of the StringUtils shutdown hook.
|
static java.lang.String |
STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG |
getTrimmedStringCollectionSplitByEquals(String) throws
IllegalArgumentException with error message starting with this string
if the argument provided is not valid representation of non-empty key-value
pairs. |
static java.util.regex.Pattern |
WIN_ENV_VAR_PATTERN |
Windows environment variables: surrounded by %.
|
| Constructor | Description |
|---|---|
StringUtils() |
| Modifier and Type | Method | Description |
|---|---|---|
static java.lang.String |
arrayToString(java.lang.String[] strs) |
Given an array of strings, return a comma-separated list of its elements.
|
static java.lang.String |
byteDesc(long len) |
a byte description of the given long interger value.
|
static java.lang.String |
byteToHexString(byte b) |
Convert a byte to a hex string.
|
static java.lang.String |
byteToHexString(byte[] bytes) |
Same as byteToHexString(bytes, 0, bytes.length).
|
static java.lang.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 java.lang.String |
camelize(java.lang.String s) |
Convert SOME_STUFF to SomeStuff
|
static java.lang.String |
createStartupShutdownMessage(java.lang.String classname,
java.lang.String hostname,
java.lang.String[] args) |
Generate the text for the startup/shutdown message of processes.
|
static boolean |
equalsIgnoreCase(java.lang.String s1,
java.lang.String s2) |
Compare strings locale-freely by using String#equalsIgnoreCase.
|
static java.lang.String |
escapeHTML(java.lang.String string) |
Escapes HTML Special characters present in the string.
|
static java.lang.String |
escapeString(java.lang.String str) |
Escape commas in the string using the default escape char
|
static java.lang.String |
escapeString(java.lang.String str,
char escapeChar,
char charToEscape) |
Escape
charToEscape in the string
with the escape char escapeChar |
static java.lang.String |
escapeString(java.lang.String str,
char escapeChar,
char[] charsToEscape) |
escapeString.
|
static int |
findNext(java.lang.String str,
char separator,
char escapeChar,
int start,
java.lang.StringBuilder split) |
Finds the first occurrence of the separator character ignoring the escaped
separators starting from the index.
|
static java.lang.String |
format(java.lang.String format,
java.lang.Object... objects) |
The same as String.format(Locale.ENGLISH, format, objects).
|
static java.lang.String |
formatPercent(double fraction,
int decimalPlaces) |
Format a percentage for presentation to the user.
|
static java.lang.String |
formatTime(long timeDiff) |
Given the time in long milliseconds, returns a
String in the format Xhrs, Ymins, Z sec.
|
static java.lang.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 java.lang.String |
formatTimeSortable(long timeDiff) |
Given the time in long milliseconds, returns a String in the sortable
format Xhrs, Ymins, Zsec.
|
static java.lang.String |
getFormattedTimeWithDiff(java.lang.String formattedFinishTime,
long finishTime,
long startTime) |
Formats time in ms and appends difference (finishTime - startTime)
as returned by formatTimeDiff().
|
static java.lang.String |
getFormattedTimeWithDiff(org.apache.commons.lang3.time.FastDateFormat dateFormat,
long finishTime,
long startTime) |
Formats time in ms and appends difference (finishTime - startTime)
as returned by formatTimeDiff().
|
static java.lang.String |
getStackTrace(java.lang.Thread t) |
Get stack trace for a given thread.
|
static java.lang.String |
getStackTrace(java.lang.Throwable t) |
Get stack trace from throwable exception.
|
static java.util.Collection<java.lang.String> |
getStringCollection(java.lang.String str) |
Returns a collection of strings.
|
static java.util.Collection<java.lang.String> |
getStringCollection(java.lang.String str,
java.lang.String delim) |
Returns a collection of strings.
|
static java.lang.String[] |
getStrings(java.lang.String str) |
Returns an arraylist of strings.
|
static java.lang.String[] |
getStrings(java.lang.String str,
java.lang.String delim) |
Returns an arraylist of strings.
|
static java.util.Collection<java.lang.String> |
getTrimmedStringCollection(java.lang.String str) |
Splits a comma separated value
String, trimming leading and
trailing whitespace on each value. |
static java.util.Collection<java.lang.String> |
getTrimmedStringCollection(java.lang.String str,
java.lang.String delim) |
Returns a collection of strings, trimming leading and trailing whitespace
on each value.
|
static java.util.Map<java.lang.String,java.lang.String> |
getTrimmedStringCollectionSplitByEquals(java.lang.String str) |
Splits an "=" separated value
String, trimming leading and
trailing whitespace on each value after splitting by comma and new line separator. |
static java.lang.String[] |
getTrimmedStrings(java.lang.String str) |
Splits a comma or newline separated value
String, trimming
leading and trailing whitespace on each value. |
static java.lang.String[] |
getTrimmedStringsSplitByEquals(java.lang.String str) |
Splits "=" separated value
String, trimming
leading and trailing whitespace on each value. |
static boolean |
hasLength(java.lang.String str) |
Checks whether the given string is not
null and has a length greater than zero. |
static byte[] |
hexStringToByte(java.lang.String hex) |
Given a hexstring this will return the byte array corresponding to the
string
|
static java.lang.String |
humanReadableInt(long number) |
Deprecated.
|
static boolean |
isAlpha(java.lang.String str) |
Checks if the String contains only unicode letters.
|
static java.lang.String |
join(char separator,
java.lang.Iterable<?> strings) |
|
static java.lang.String |
join(char separator,
java.lang.String[] strings) |
|
static java.lang.String |
join(java.lang.CharSequence separator,
java.lang.Iterable<?> strings) |
Concatenates strings, using a separator.
|
static java.lang.String |
join(java.lang.CharSequence separator,
java.lang.String[] strings) |
Concatenates strings, using a separator.
|
static java.lang.String |
limitDecimalTo2(double d) |
Deprecated.
use StringUtils.format("%.2f", d).
|
static java.lang.String |
popFirstNonOption(java.util.List<java.lang.String> args) |
From a list of command-line arguments, return the first non-option
argument.
|
static boolean |
popOption(java.lang.String name,
java.util.List<java.lang.String> args) |
From a list of command-line arguments, remove an option.
|
static java.lang.String |
popOptionWithArgument(java.lang.String name,
java.util.List<java.lang.String> args) |
From a list of command-line arguments, remove both an option and the
next argument.
|
static java.lang.String |
replaceTokens(java.lang.String template,
java.util.regex.Pattern pattern,
java.util.Map<java.lang.String,java.lang.String> replacements) |
Matches a template string against a pattern, replaces matched tokens with
the supplied replacements, and returns the result.
|
static java.lang.String |
simpleHostname(java.lang.String fullHostname) |
Given a full hostname, return the word upto the first dot.
|
static java.lang.String[] |
split(java.lang.String str) |
Split a string using the default separator
|
static java.lang.String[] |
split(java.lang.String str,
char separator) |
Split a string using the given separator, with no escaping performed.
|
static java.lang.String[] |
split(java.lang.String str,
char escapeChar,
char separator) |
Split a string using the given separator
|
static void |
startupShutdownMessage(java.lang.Class<?> clazz,
java.lang.String[] args,
org.slf4j.Logger log) |
Print a log message for starting up and shutting down
|
static java.lang.String |
stringifyException(java.lang.Throwable e) |
Make a string representation of the exception.
|
static Path[] |
stringToPath(java.lang.String[] str) |
stringToPath.
|
static java.net.URI[] |
stringToURI(java.lang.String[] str) |
|
static java.lang.String |
toLowerCase(java.lang.String str) |
Converts all of the characters in this String to lower case with
Locale.ENGLISH.
|
static java.lang.String |
toStartupShutdownString(java.lang.String prefix,
java.lang.String[] msg) |
Return a message for logging.
|
static java.lang.String |
toUpperCase(java.lang.String str) |
Converts all of the characters in this String to upper case with
Locale.ENGLISH.
|
static java.lang.String |
unEscapeString(java.lang.String str) |
Unescape commas in the string using the default escape char
|
static java.lang.String |
unEscapeString(java.lang.String str,
char escapeChar,
char charToEscape) |
Unescape
charToEscape in the string
with the escape char escapeChar |
static java.lang.String |
unEscapeString(java.lang.String str,
char escapeChar,
char[] charsToEscape) |
unEscapeString.
|
static java.lang.String |
uriToString(java.net.URI[] uris) |
uriToString.
|
static java.lang.String |
wrap(java.lang.String str,
int wrapLength,
java.lang.String newLineStr,
boolean wrapLongWords) |
Same as WordUtils#wrap in commons-lang 2.6.
|
public static final int SHUTDOWN_HOOK_PRIORITY
public static final java.util.regex.Pattern SHELL_ENV_VAR_PATTERN
public static final java.util.regex.Pattern WIN_ENV_VAR_PATTERN
public static final java.util.regex.Pattern ENV_VAR_PATTERN
@VisibleForTesting public static final java.lang.String STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG
getTrimmedStringCollectionSplitByEquals(String) throws
IllegalArgumentException with error message starting with this string
if the argument provided is not valid representation of non-empty key-value
pairs.
Value = "Trimmed string split by equals does not correctly represent non-empty key-value pairs."public static final java.lang.String[] emptyStringArray
public static final char COMMA
public static final java.lang.String COMMA_STR
public static final char ESCAPE_CHAR
public static java.lang.String stringifyException(java.lang.Throwable e)
e - The exception to stringifypublic static java.lang.String simpleHostname(java.lang.String fullHostname)
fullHostname - the full hostname@Deprecated public static java.lang.String humanReadableInt(long number)
number - the number to formatpublic static java.lang.String format(java.lang.String format,
java.lang.Object... objects)
format - format.objects - objects.public static java.lang.String formatPercent(double fraction,
int decimalPlaces)
fraction - the percentage as a fraction, e.g. 0.1 = 10%decimalPlaces - the number of decimal placespublic static java.lang.String arrayToString(java.lang.String[] strs)
strs - Array of stringspublic static java.lang.String byteToHexString(byte[] bytes,
int start,
int end)
bytes - bytes.start - start index, inclusivelyend - end index, exclusivelypublic static java.lang.String byteToHexString(byte[] bytes)
bytes - bytes.public static java.lang.String byteToHexString(byte b)
b - bytebyteToHexString(byte[]),
byteToHexString(byte[], int, int)public static byte[] hexStringToByte(java.lang.String hex)
hex - the hex String arraypublic static java.lang.String uriToString(java.net.URI[] uris)
uris - uris.public static java.net.URI[] stringToURI(java.lang.String[] str)
str - The string array to be parsed into an URI array.java.lang.IllegalArgumentException - If any string in str violates RFC 2396.public static Path[] stringToPath(java.lang.String[] str)
str - str.public static java.lang.String formatTimeDiff(long finishTime,
long startTime)
finishTime - finish timestartTime - start timepublic static java.lang.String formatTime(long timeDiff)
timeDiff - The time difference to formatpublic static java.lang.String formatTimeSortable(long timeDiff)
timeDiff - The time difference to formatpublic static java.lang.String getFormattedTimeWithDiff(org.apache.commons.lang3.time.FastDateFormat dateFormat,
long finishTime,
long startTime)
dateFormat - date format to usefinishTime - finish timestartTime - start timepublic static java.lang.String getFormattedTimeWithDiff(java.lang.String formattedFinishTime,
long finishTime,
long startTime)
formattedFinishTime - formattedFinishTime to usefinishTime - finish timestartTime - start timepublic static java.lang.String[] getStrings(java.lang.String str)
str - the comma separated string valuespublic static java.lang.String[] getStrings(java.lang.String str,
java.lang.String delim)
str - the string valuesdelim - delimiter to separate the valuespublic static java.util.Collection<java.lang.String> getStringCollection(java.lang.String str)
str - comma separated string valuesArrayList of string valuespublic static java.util.Collection<java.lang.String> getStringCollection(java.lang.String str,
java.lang.String delim)
str - String to parsedelim - delimiter to separate the valuespublic static java.util.Collection<java.lang.String> getTrimmedStringCollection(java.lang.String str,
java.lang.String delim)
str - String separated by delim.delim - Delimiter to separate the values in str.public static java.util.Collection<java.lang.String> getTrimmedStringCollection(java.lang.String str)
String, trimming leading and
trailing whitespace on each value. Duplicate and empty values are removed.str - a comma separated String with values, may be nullCollection of String values, empty
Collection if null String inputpublic static java.util.Map<java.lang.String,java.lang.String> getTrimmedStringCollectionSplitByEquals(java.lang.String str)
String, trimming leading and
trailing whitespace on each value after splitting by comma and new line separator.str - a comma separated String with values, may be nullMap of String keys and values, empty
Collection if null String input.public static java.lang.String[] getTrimmedStrings(java.lang.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 java.lang.String[] getTrimmedStringsSplitByEquals(java.lang.String str)
String, trimming
leading and trailing whitespace on each value.str - an "=" separated String with values,
may be nullString values, empty array if null String
inputpublic static java.lang.String[] split(java.lang.String str)
str - a string that may have escaped separatorpublic static java.lang.String[] split(java.lang.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 java.lang.String[] split(java.lang.String str,
char separator)
str - a string to be split. Note that this may not be null.separator - a separator charpublic static int findNext(java.lang.String str,
char separator,
char escapeChar,
int start,
java.lang.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 java.lang.String escapeString(java.lang.String str)
str - a stringpublic static java.lang.String escapeString(java.lang.String str,
char escapeChar,
char charToEscape)
charToEscape in the string
with the escape char escapeCharstr - stringescapeChar - escape charcharToEscape - the char to be escapedpublic static java.lang.String escapeString(java.lang.String str,
char escapeChar,
char[] charsToEscape)
str - str.escapeChar - escapeChar.charsToEscape - array of characters to be escapedpublic static java.lang.String unEscapeString(java.lang.String str)
str - a stringpublic static java.lang.String unEscapeString(java.lang.String str,
char escapeChar,
char charToEscape)
charToEscape in the string
with the escape char escapeCharstr - stringescapeChar - escape charcharToEscape - the escaped charpublic static java.lang.String unEscapeString(java.lang.String str,
char escapeChar,
char[] charsToEscape)
str - str.escapeChar - escapeChar.charsToEscape - array of characters to unescapepublic static java.lang.String toStartupShutdownString(java.lang.String prefix,
java.lang.String[] msg)
prefix - prefix keyword for the messagemsg - content of the messagepublic static void startupShutdownMessage(java.lang.Class<?> clazz,
java.lang.String[] args,
org.slf4j.Logger log)
clazz - the class of the serverargs - argumentslog - the target log objectpublic static java.lang.String createStartupShutdownMessage(java.lang.String classname,
java.lang.String hostname,
java.lang.String[] args)
classname - short classname of the classhostname - hostnameargs - Command argumentspublic static java.lang.String escapeHTML(java.lang.String string)
string - param string.public static java.lang.String byteDesc(long len)
len - len.@Deprecated public static java.lang.String limitDecimalTo2(double d)
d - double param.public static java.lang.String join(java.lang.CharSequence separator,
java.lang.Iterable<?> strings)
separator - Separator to join with.strings - Strings to join.public static java.lang.String join(char separator,
java.lang.Iterable<?> strings)
public static java.lang.String join(java.lang.CharSequence separator,
java.lang.String[] strings)
separator - to join withstrings - to joinpublic static java.lang.String join(char separator,
java.lang.String[] strings)
public static java.lang.String camelize(java.lang.String s)
s - input stringpublic static java.lang.String replaceTokens(java.lang.String template,
java.util.regex.Pattern pattern,
java.util.Map<java.lang.String,java.lang.String> replacements)
template - String template to receive replacementspattern - Pattern to match for identifying tokens, must use a capturing
groupreplacements - Map<String, String> mapping tokens identified by
the capturing group to their replacement valuespublic static java.lang.String getStackTrace(java.lang.Thread t)
t - thread.public static java.lang.String getStackTrace(java.lang.Throwable t)
t - Throwable.public static java.lang.String popOptionWithArgument(java.lang.String name,
java.util.List<java.lang.String> args)
throws java.lang.IllegalArgumentException
name - Name of the option to remove. Example: -foo.args - List of arguments.java.lang.IllegalArgumentException - if the option's argument is not presentpublic static boolean popOption(java.lang.String name,
java.util.List<java.lang.String> args)
name - Name of the option to remove. Example: -foo.args - List of arguments.public static java.lang.String popFirstNonOption(java.util.List<java.lang.String> args)
args - List of arguments.public static java.lang.String toLowerCase(java.lang.String str)
str - string to be convertedpublic static java.lang.String toUpperCase(java.lang.String str)
str - string to be convertedpublic static boolean equalsIgnoreCase(java.lang.String s1,
java.lang.String s2)
s1 - Non-null string to be converteds2 - string to be convertedpublic static boolean isAlpha(java.lang.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-nullpublic static java.lang.String wrap(java.lang.String str,
int wrapLength,
java.lang.String newLineStr,
boolean wrapLongWords)
str - the String to be word wrapped, may be nullwrapLength - the column to wrap the words at, less than 1 is treated
as 1newLineStr - the string to insert for a new line,
null uses the system property line separatorwrapLongWords - true if long words (such as URLs) should be wrappednull if null inputpublic static boolean hasLength(java.lang.String str)
null and has a length greater than zero.str - the string to checktrue if the string is not null and not empty; false otherwiseCopyright © 2008–2025 Apache Software Foundation. All rights reserved.