org.apache.hadoop.util
Class StringUtils

java.lang.Object
  extended by org.apache.hadoop.util.StringUtils

public class StringUtils
extends Object

General string utils


Nested Class Summary
static class StringUtils.TraditionalBinaryPrefix
          The traditional binary prefixes, kilo, mega, ..., exa, which can be represented by a 64-bit integer.
 
Field Summary
static char COMMA
           
static String COMMA_STR
           
static char ESCAPE_CHAR
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static String arrayToString(String[] strs)
          Given an array of strings, return a comma-separated list of its elements.
static String byteDesc(long len)
          Return an abbreviated English-language desc of the byte length
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 String capitalize(String s)
          Capitalize a word
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 formatPercent(double done, int digits)
          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 getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)
          Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff().
static String getHostname()
          Return hostname without throwing exception.
static Collection<String> getStringCollection(String str)
          Returns a collection of strings.
static String[] getStrings(String str)
          Returns an arraylist of strings.
static byte[] hexStringToByte(String hex)
          Given a hexstring this will return the byte array corresponding to the string
static String humanReadableInt(long number)
          Given an integer, return a string that is in an approximate, but human readable format.
static String join(CharSequence separator, Iterable<String> strings)
          Concatenates strings, using a separator.
static String join(CharSequence separator, Object[] objects)
          Concatenates objects, using a separator.
static String join(CharSequence separator, String[] strings)
          Concatenates strings, using a separator.
static String limitDecimalTo2(double d)
           
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 escapeChar, char separator)
          Split a string using the given separator
static void startupShutdownMessage(Class<?> clazz, String[] args, org.apache.commons.logging.Log LOG)
          Print a log message for starting up and shutting down
static String stringifyException(Throwable e)
          Make a string representation of the exception.
static Path[] stringToPath(String[] str)
           
static URI[] stringToURI(String[] str)
           
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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMA

public static final char COMMA
See Also:
Constant Field Values

COMMA_STR

public static final String COMMA_STR
See Also:
Constant Field Values

ESCAPE_CHAR

public static final char ESCAPE_CHAR
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

stringifyException

public static String stringifyException(Throwable e)
Make a string representation of the exception.

Parameters:
e - The exception to stringify
Returns:
A string with exception name and call stack.

simpleHostname

public static String simpleHostname(String fullHostname)
Given a full hostname, return the word upto the first dot.

Parameters:
fullHostname - the full hostname
Returns:
the hostname to the first dot

humanReadableInt

public static String humanReadableInt(long number)
Given an integer, return a string that is in an approximate, but human readable format. It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.

Parameters:
number - the number to format
Returns:
a human readable form of the integer

formatPercent

public static String formatPercent(double done,
                                   int digits)
Format a percentage for presentation to the user.

Parameters:
done - the percentage to format (0.0 to 1.0)
digits - the number of digits past the decimal point
Returns:
a string representation of the percentage

arrayToString

public static String arrayToString(String[] strs)
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

public 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

Parameters:
bytes -
start - start index, inclusively
end - end index, exclusively
Returns:
hex string representation of the byte array

byteToHexString

public static String byteToHexString(byte[] bytes)
Same as byteToHexString(bytes, 0, bytes.length).


hexStringToByte

public static byte[] hexStringToByte(String hex)
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

public static String uriToString(URI[] uris)
Parameters:
uris -

stringToURI

public static URI[] stringToURI(String[] str)
Parameters:
str -

stringToPath

public static Path[] stringToPath(String[] str)
Parameters:
str -

formatTimeDiff

public 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. If finish time comes before start time then negative valeus of X, Y and Z wil return.

Parameters:
finishTime - finish time
startTime - start time

formatTime

public static String formatTime(long timeDiff)
Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.

Parameters:
timeDiff - The time difference to format

getFormattedTimeWithDiff

public static String getFormattedTimeWithDiff(DateFormat 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 use
finishTime - fnish time
startTime - start time
Returns:
formatted value.

getStrings

public static String[] getStrings(String str)
Returns an arraylist of strings.

Parameters:
str - the comma seperated string values
Returns:
the arraylist of the comma seperated string values

getStringCollection

public static Collection<String> getStringCollection(String str)
Returns a collection of strings.

Parameters:
str - comma seperated string values
Returns:
an ArrayList of string values

split

public static String[] split(String str)
Split a string using the default separator

Parameters:
str - a string that may have escaped separator
Returns:
an array of strings

split

public static String[] split(String str,
                             char escapeChar,
                             char separator)
Split a string using the given separator

Parameters:
str - a string that may have escaped separator
escapeChar - a char that be used to escape the separator
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 string
separator - the character to find
escapeChar - character used to escape
start - from where to search
split - used to pass back the extracted string

escapeString

public static String escapeString(String str)
Escape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an escaped string

escapeString

public static String escapeString(String str,
                                  char escapeChar,
                                  char charToEscape)
Escape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the char to be escaped
Returns:
an escaped string

escapeString

public static String escapeString(String str,
                                  char escapeChar,
                                  char[] charsToEscape)
Parameters:
charsToEscape - array of characters to be escaped

unEscapeString

public static String unEscapeString(String str)
Unescape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an unescaped string

unEscapeString

public static String unEscapeString(String str,
                                    char escapeChar,
                                    char charToEscape)
Unescape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the escaped char
Returns:
an unescaped string

unEscapeString

public static String unEscapeString(String str,
                                    char escapeChar,
                                    char[] charsToEscape)
Parameters:
charsToEscape - array of characters to unescape

getHostname

public static String getHostname()
Return hostname without throwing exception.

Returns:
hostname

startupShutdownMessage

public static void startupShutdownMessage(Class<?> clazz,
                                          String[] args,
                                          org.apache.commons.logging.Log LOG)
Print a log message for starting up and shutting down

Parameters:
clazz - the class of the server
args - arguments
LOG - the target log object

escapeHTML

public static String escapeHTML(String string)
Escapes HTML Special characters present in the string.

Parameters:
string -
Returns:
HTML Escaped String representation

byteDesc

public static String byteDesc(long len)
Return an abbreviated English-language desc of the byte length


limitDecimalTo2

public static String limitDecimalTo2(double d)

join

public static String join(CharSequence separator,
                          Iterable<String> strings)
Concatenates strings, using a separator.

Parameters:
separator - Separator to join with.
strings - Strings to join.
Returns:
the joined string

join

public static String join(CharSequence separator,
                          String[] strings)
Concatenates strings, using a separator.

Parameters:
separator - to join with
strings - to join
Returns:
the joined string

join

public static String join(CharSequence separator,
                          Object[] objects)
Concatenates objects, using a separator.

Parameters:
separator - to join with
objects - to join
Returns:
the joined string

capitalize

public static String capitalize(String s)
Capitalize a word

Parameters:
s - the input string
Returns:
capitalized string

camelize

public static String camelize(String s)
Convert SOME_STUFF to SomeStuff

Parameters:
s - input string
Returns:
camelized string


Copyright © 2009 The Apache Software Foundation