Package org.apache.hadoop.util
Class StringUtils
java.lang.Object
org.apache.hadoop.util.StringUtils
General string utils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe traditional binary prefixes, kilo, mega, ..., exa, which can be represented by a 64-bit integer. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charstatic final Stringstatic final String[]static final PatternRegular expression that matches and captures environment variable names according to platform-specific rules.static final charstatic final PatternShell environment variables: $ followed by one letter or _ followed by multiple letters, numbers, or underscores.static final intPriority of the StringUtils shutdown hook.static final StringgetTrimmedStringCollectionSplitByEquals(String)throwsIllegalArgumentExceptionwith error message starting with this string if the argument provided is not valid representation of non-empty key-value pairs.static final PatternWindows environment variables: surrounded by %. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringarrayToString(String[] strs) Given an array of strings, return a comma-separated list of its elements.static StringbyteDesc(long len) a byte description of the given long interger value.static StringbyteToHexString(byte b) Convert a byte to a hex string.static StringbyteToHexString(byte[] bytes) Same as byteToHexString(bytes, 0, bytes.length).static StringbyteToHexString(byte[] bytes, int start, int end) Given an array of bytes it will convert the bytes to a hex string representation of the bytesstatic StringConvert SOME_STUFF to SomeStuffstatic StringcreateStartupShutdownMessage(String classname, String hostname, String[] args) Generate the text for the startup/shutdown message of processes.static booleanequalsIgnoreCase(String s1, String s2) Compare strings locale-freely by using String#equalsIgnoreCase.static StringescapeHTML(String string) Escapes HTML Special characters present in the string.static StringescapeString(String str) Escape commas in the string using the default escape charstatic StringescapeString(String str, char escapeChar, char charToEscape) EscapecharToEscapein the string with the escape charescapeCharstatic StringescapeString(String str, char escapeChar, char[] charsToEscape) escapeString.static intfindNext(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 StringThe same as String.format(Locale.ENGLISH, format, objects).static StringformatPercent(double fraction, int decimalPlaces) Format a percentage for presentation to the user.static StringformatTime(long timeDiff) Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.static StringformatTimeDiff(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 StringformatTimeSortable(long timeDiff) Given the time in long milliseconds, returns a String in the sortable format Xhrs, Ymins, Zsec.static StringgetFormattedTimeWithDiff(String formattedFinishTime, long finishTime, long startTime) Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff().static StringgetFormattedTimeWithDiff(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 StringGet stack trace for a given thread.static StringGet stack trace from throwable exception.static Collection<String>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>Splits a comma separated valueString, trimming leading and trailing whitespace on each value.static Collection<String>getTrimmedStringCollection(String str, String delim) Returns a collection of strings, trimming leading and trailing whitespace on each value.Splits an "=" separated valueString, trimming leading and trailing whitespace on each value after splitting by comma and new line separator.static String[]getTrimmedStrings(String str) Splits a comma or newline separated valueString, trimming leading and trailing whitespace on each value.static String[]Splits "=" separated valueString, trimming leading and trailing whitespace on each value.static booleanChecks whether the given string is notnulland has a length greater than zero.static byte[]hexStringToByte(String hex) Given a hexstring this will return the byte array corresponding to the stringstatic StringhumanReadableInt(long number) Deprecated.static booleanChecks if the String contains only unicode letters.static Stringstatic Stringstatic Stringjoin(CharSequence separator, Iterable<?> strings) Concatenates strings, using a separator.static Stringjoin(CharSequence separator, String[] strings) Concatenates strings, using a separator.static StringlimitDecimalTo2(double d) Deprecated.use StringUtils.format("%.2f", d).static StringpopFirstNonOption(List<String> args) From a list of command-line arguments, return the first non-option argument.static booleanFrom a list of command-line arguments, remove an option.static StringpopOptionWithArgument(String name, List<String> args) From a list of command-line arguments, remove both an option and the next argument.static StringMatches a template string against a pattern, replaces matched tokens with the supplied replacements, and returns the result.static StringsimpleHostname(String fullHostname) Given a full hostname, return the word upto the first dot.static String[]Split a string using the default separatorstatic String[]Split a string using the given separator, with no escaping performed.static String[]Split a string using the given separatorstatic voidstartupShutdownMessage(Class<?> clazz, String[] args, org.slf4j.Logger log) Print a log message for starting up and shutting downstatic StringMake a string representation of the exception.static Path[]stringToPath(String[] str) stringToPath.static URI[]stringToURI(String[] str) static StringtoLowerCase(String str) Converts all of the characters in this String to lower case with Locale.ENGLISH.static StringtoStartupShutdownString(String prefix, String[] msg) Return a message for logging.static StringtoUpperCase(String str) Converts all of the characters in this String to upper case with Locale.ENGLISH.static StringunEscapeString(String str) Unescape commas in the string using the default escape charstatic StringunEscapeString(String str, char escapeChar, char charToEscape) UnescapecharToEscapein the string with the escape charescapeCharstatic StringunEscapeString(String str, char escapeChar, char[] charsToEscape) unEscapeString.static StringuriToString(URI[] uris) uriToString.static StringSame as WordUtils#wrap in commons-lang 2.6.
-
Field Details
-
SHUTDOWN_HOOK_PRIORITY
public static final int SHUTDOWN_HOOK_PRIORITYPriority of the StringUtils shutdown hook.- See Also:
-
SHELL_ENV_VAR_PATTERN
Shell environment variables: $ followed by one letter or _ followed by multiple letters, numbers, or underscores. The group captures the environment variable name without the leading $. -
WIN_ENV_VAR_PATTERN
Windows environment variables: surrounded by %. The group captures the environment variable name without the leading and trailing %. -
ENV_VAR_PATTERN
Regular expression that matches and captures environment variable names according to platform-specific rules. -
STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG
getTrimmedStringCollectionSplitByEquals(String)throwsIllegalArgumentExceptionwith 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."- See Also:
-
emptyStringArray
-
COMMA
public static final char COMMA- See Also:
-
COMMA_STR
- See Also:
-
ESCAPE_CHAR
public static final char ESCAPE_CHAR- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
stringifyException
Make a string representation of the exception.- Parameters:
e- The exception to stringify- Returns:
- A string with exception name and call stack.
-
simpleHostname
Given a full hostname, return the word upto the first dot.- Parameters:
fullHostname- the full hostname- Returns:
- the hostname to the first dot
-
humanReadableInt
Deprecated.Given an integer, return a string that is in an approximate, but human readable format.- Parameters:
number- the number to format- Returns:
- a human readable form of the integer
-
format
The same as String.format(Locale.ENGLISH, format, objects).- Parameters:
format- format.objects- objects.- Returns:
- format string.
-
formatPercent
Format a percentage for presentation to the user.- Parameters:
fraction- the percentage as a fraction, e.g. 0.1 = 10%decimalPlaces- the number of decimal places- Returns:
- a string representation of the percentage
-
arrayToString
Given an array of strings, return a comma-separated list of its elements.- Parameters:
strs- Array of strings- Returns:
- Empty string if strs.length is 0, comma separated list of strings otherwise
-
byteToHexString
Given an array of bytes it will convert the bytes to a hex string representation of the bytes- Parameters:
bytes- bytes.start- start index, inclusivelyend- end index, exclusively- Returns:
- hex string representation of the byte array
-
byteToHexString
Same as byteToHexString(bytes, 0, bytes.length).- Parameters:
bytes- bytes.- Returns:
- byteToHexString.
-
byteToHexString
Convert a byte to a hex string.- Parameters:
b- byte- Returns:
- byte's hex value as a String
- See Also:
-
hexStringToByte
Given a hexstring this will return the byte array corresponding to the string- Parameters:
hex- the hex String array- Returns:
- a byte array that is a hex string representation of the given string. The size of the byte array is therefore hex.length/2
-
uriToString
uriToString.- Parameters:
uris- uris.- Returns:
- uriToString.
-
stringToURI
- Parameters:
str- The string array to be parsed into an URI array.- Returns:
nullif str isnull, else the URI array equivalent to str.- Throws:
IllegalArgumentException- If any string in str violates RFC 2396.
-
stringToPath
stringToPath.- Parameters:
str- str.- Returns:
- path array.
-
formatTimeDiff
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. If finish time comes before start time then negative valeus of X, Y and Z wil return.- Parameters:
finishTime- finish timestartTime- start time- Returns:
- a String in the format Xhrs, Ymins, Z sec, for the time difference between two times.
-
formatTime
Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.- Parameters:
timeDiff- The time difference to format- Returns:
- formatTime String.
-
formatTimeSortable
Given the time in long milliseconds, returns a String in the sortable format Xhrs, Ymins, Zsec. X, Y, and Z are always two-digit. If the time is more than 100 hours ,it is displayed as 99hrs, 59mins, 59sec.- Parameters:
timeDiff- The time difference to format- Returns:
- format time sortable.
-
getFormattedTimeWithDiff
public static 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(). If finish time is 0, empty string is returned, if start time is 0 then difference is not appended to return value.- Parameters:
dateFormat- date format to usefinishTime- finish timestartTime- start time- Returns:
- formatted value.
-
getFormattedTimeWithDiff
public static String getFormattedTimeWithDiff(String formattedFinishTime, long finishTime, long startTime) Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff(). If finish time is 0, empty string is returned, if start time is 0 then difference is not appended to return value.- Parameters:
formattedFinishTime- formattedFinishTime to usefinishTime- finish timestartTime- start time- Returns:
- formatted value.
-
getStrings
Returns an arraylist of strings.- Parameters:
str- the comma separated string values- Returns:
- the arraylist of the comma separated string values
-
getStrings
Returns an arraylist of strings.- Parameters:
str- the string valuesdelim- delimiter to separate the values- Returns:
- the arraylist of the separated string values
-
getStringCollection
Returns a collection of strings.- Parameters:
str- comma separated string values- Returns:
- an
ArrayListof string values
-
getStringCollection
Returns a collection of strings.- Parameters:
str- String to parsedelim- delimiter to separate the values- Returns:
- Collection of parsed elements.
-
getTrimmedStringCollection
Returns a collection of strings, trimming leading and trailing whitespace on each value. Duplicates are not removed.- Parameters:
str- String separated by delim.delim- Delimiter to separate the values in str.- Returns:
- Collection of string values.
-
getTrimmedStringCollection
Splits a comma separated valueString, trimming leading and trailing whitespace on each value. Duplicate and empty values are removed.- Parameters:
str- a comma separatedStringwith values, may be null- Returns:
- a
CollectionofStringvalues, empty Collection if null String input
-
getTrimmedStringCollectionSplitByEquals
Splits an "=" separated valueString, trimming leading and trailing whitespace on each value after splitting by comma and new line separator.- Parameters:
str- a comma separatedStringwith values, may be null- Returns:
- a
MapofStringkeys and values, empty Collection if null String input.
-
getTrimmedStrings
Splits a comma or newline separated valueString, trimming leading and trailing whitespace on each value.- Parameters:
str- a comma or newline separatedStringwith values, may be null- Returns:
- an array of
Stringvalues, empty array if null String input
-
getTrimmedStringsSplitByEquals
Splits "=" separated valueString, trimming leading and trailing whitespace on each value.- Parameters:
str- an "=" separatedStringwith values, may be null- Returns:
- an array of
Stringvalues, empty array if null String input
-
split
Split a string using the default separator- Parameters:
str- a string that may have escaped separator- Returns:
- an array of strings
-
split
Split a string using the given separator- Parameters:
str- a string that may have escaped separatorescapeChar- a char that be used to escape the separatorseparator- a separator char- Returns:
- an array of strings
-
split
Split a string using the given separator, with no escaping performed.- Parameters:
str- a string to be split. Note that this may not be null.separator- a separator char- Returns:
- an array of strings
-
findNext
public 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. Note the substring between the index and the position of the separator is passed.- Parameters:
str- the source stringseparator- the character to findescapeChar- character used to escapestart- from where to searchsplit- used to pass back the extracted string- Returns:
- index.
-
escapeString
Escape commas in the string using the default escape char- Parameters:
str- a string- Returns:
- an escaped string
-
escapeString
EscapecharToEscapein the string with the escape charescapeChar- Parameters:
str- stringescapeChar- escape charcharToEscape- the char to be escaped- Returns:
- an escaped string
-
escapeString
escapeString.- Parameters:
str- str.escapeChar- escapeChar.charsToEscape- array of characters to be escaped- Returns:
- escapeString.
-
unEscapeString
Unescape commas in the string using the default escape char- Parameters:
str- a string- Returns:
- an unescaped string
-
unEscapeString
UnescapecharToEscapein the string with the escape charescapeChar- Parameters:
str- stringescapeChar- escape charcharToEscape- the escaped char- Returns:
- an unescaped string
-
unEscapeString
unEscapeString.- Parameters:
str- str.escapeChar- escapeChar.charsToEscape- array of characters to unescape- Returns:
- escape string.
-
toStartupShutdownString
Return a message for logging.- Parameters:
prefix- prefix keyword for the messagemsg- content of the message- Returns:
- a message for logging
-
startupShutdownMessage
Print a log message for starting up and shutting down- Parameters:
clazz- the class of the serverargs- argumentslog- the target log object
-
createStartupShutdownMessage
Generate the text for the startup/shutdown message of processes.- Parameters:
classname- short classname of the classhostname- hostnameargs- Command arguments- Returns:
- a string to log.
-
escapeHTML
Escapes HTML Special characters present in the string.- Parameters:
string- param string.- Returns:
- HTML Escaped String representation
-
byteDesc
a byte description of the given long interger value.- Parameters:
len- len.- Returns:
- a byte description of the given long interger value.
-
limitDecimalTo2
Deprecated.use StringUtils.format("%.2f", d).limitDecimalTo2.- Parameters:
d- double param.- Returns:
- string value ("%.2f").
-
join
Concatenates strings, using a separator.- Parameters:
separator- Separator to join with.strings- Strings to join.- Returns:
- join string.
-
join
-
join
Concatenates strings, using a separator.- Parameters:
separator- to join withstrings- to join- Returns:
- the joined string
-
join
-
camelize
Convert SOME_STUFF to SomeStuff- Parameters:
s- input string- Returns:
- camelized string
-
replaceTokens
public 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. The regular expression must use a capturing group. The value of the first capturing group is used to look up the replacement. If no replacement is found for the token, then it is replaced with the empty string. For example, assume template is "%foo%_%bar%_%baz%", pattern is "%(.*?)%", and replacements contains 2 entries, mapping "foo" to "zoo" and "baz" to "zaz". The result returned would be "zoo__zaz".- Parameters:
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 values- Returns:
- String template with replacements
-
getStackTrace
Get stack trace for a given thread.- Parameters:
t- thread.- Returns:
- stack trace string.
-
getStackTrace
Get stack trace from throwable exception.- Parameters:
t- Throwable.- Returns:
- stack trace string.
-
popOptionWithArgument
public static String popOptionWithArgument(String name, List<String> args) throws IllegalArgumentException From a list of command-line arguments, remove both an option and the next argument.- Parameters:
name- Name of the option to remove. Example: -foo.args- List of arguments.- Returns:
- null if the option was not found; the value of the option otherwise.
- Throws:
IllegalArgumentException- if the option's argument is not present
-
popOption
From a list of command-line arguments, remove an option.- Parameters:
name- Name of the option to remove. Example: -foo.args- List of arguments.- Returns:
- true if the option was found and removed; false otherwise.
-
popFirstNonOption
From a list of command-line arguments, return the first non-option argument. Non-option arguments are those which either come after a double dash (--) or do not start with a dash.- Parameters:
args- List of arguments.- Returns:
- The first non-option argument, or null if there were none.
-
toLowerCase
Converts all of the characters in this String to lower case with Locale.ENGLISH.- Parameters:
str- string to be converted- Returns:
- the str, converted to lowercase.
-
toUpperCase
Converts all of the characters in this String to upper case with Locale.ENGLISH.- Parameters:
str- string to be converted- Returns:
- the str, converted to uppercase.
-
equalsIgnoreCase
Compare strings locale-freely by using String#equalsIgnoreCase.- Parameters:
s1- Non-null string to be converteds2- string to be converted- Returns:
- the str, converted to uppercase.
-
isAlpha
Checks if the String contains only unicode letters.
nullwill returnfalse. An empty String (length()=0) will returntrue.StringUtils.isAlpha(null) = false StringUtils.isAlpha("") = true StringUtils.isAlpha(" ") = false StringUtils.isAlpha("abc") = true StringUtils.isAlpha("ab2c") = false StringUtils.isAlpha("ab-c") = false- Parameters:
str- the String to check, may be null- Returns:
trueif only contains letters, and is non-null
-
wrap
Same as WordUtils#wrap in commons-lang 2.6. Unlike commons-lang3, leading spaces on the first line are NOT stripped.- Parameters:
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,nulluses the system property line separatorwrapLongWords- true if long words (such as URLs) should be wrapped- Returns:
- a line with newlines inserted,
nullif null input
-
hasLength
Checks whether the given string is notnulland has a length greater than zero.- Parameters:
str- the string to check- Returns:
trueif the string is notnulland not empty;falseotherwise
-
StringUtils.TraditionalBinaryPrefix.long2String(long, String, int).