com.xmlmind.util
Class StringUtil

java.lang.Object
  extended by com.xmlmind.util.StringUtil

public final class StringUtil
extends java.lang.Object

A collection of utility functions (static methods) operating on Strings.


Field Summary
static java.lang.String[] EMPTY_LIST
          A ready-to-use empty list of Strings.
 
Method Summary
static java.lang.String capitalize(java.lang.String string)
          Returns the specified string with its first character converted to upper case.
static java.lang.String escape(char c)
          Returns the \\uXXXX Java escape sequence corresponding to specified character.
static void escape(char c, java.lang.StringBuilder buffer)
          Same as escape(char) expect that the \\uXXXX Java escape sequence is appended to specified buffer.
static java.lang.String escape(java.lang.String string)
          Returns the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences (that is '\n', 'é', etc).
static void escape(java.lang.String string, java.lang.StringBuilder buffer)
          Same as escape(String) except that the escaped string is appended to specified buffer.
static java.lang.String join(char separatorChar, java.lang.String... strings)
          Equivalent to join(Character.toString(separatorChar), strings).
static java.lang.String join(java.lang.String separator, java.lang.String... strings)
          Joins the items of the specified list of Strings using specified separator.
static java.lang.String joinArguments(java.lang.String... args)
          Inverse operation of splitArguments(java.lang.String).
static java.lang.String quote(java.lang.String string)
          Like escape(java.lang.String) but puts a double quote character ('\"') around the escaped string.
static java.lang.String quoteArgument(java.lang.String arg)
          Quotes specified string using '\"' if needed to.
static java.lang.String replaceAll(java.lang.String string, java.lang.String oldSub, java.lang.String newSub)
          Replaces substring oldSub by substring newSub inside String string.
static java.lang.String[] split(java.lang.String s)
          Splits specified string at whitespace character boundaries.
static java.lang.String[] split(java.lang.String string, char separatorChar)
          Splits String string at occurrences of char separatorChar.
static java.lang.String[] splitArguments(java.lang.String string)
          Splits specified string in a manner which is similar to what is done for command line arguments.
static java.lang.String substituteVars(java.lang.String text, char[] names, java.lang.Object[] values)
          Equivalent to substituteVars(text, names, values, null, null, false).
static java.lang.String substituteVars(java.lang.String text, char[] names, java.lang.Object[] values, java.lang.String[] args, java.lang.String allArgs)
          Equivalent to substituteVars(text, names, values, args, allArgs, false).
static java.lang.String substituteVars(java.lang.String text, char[] names, java.lang.Object[] values, java.lang.String[] args, java.lang.String allArgs, boolean allowForeignVars)
          Returns specified text where %0, %1, ..., %9, %* and %X, %Y, etc, variables have been subsituted by specified values.
static java.lang.String uncapitalize(java.lang.String string)
          Returns the specified string with its first character converted to lower case.
static java.lang.String unescape(java.lang.String string)
          Returns the specified string with Java escape sequences (that is '\n', 'é', etc) replaced by the corresponding character.
static java.lang.String unquote(java.lang.String string)
          Like unescape(java.lang.String) but removes the double quote characters ('\"'), if any, before unescaping the string.
static java.lang.String[] wordWrap(java.lang.String text, int maxLineLength)
          Splits specified string at word boundaries, returning an array of lines having at most specified number of characters.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_LIST

public static final java.lang.String[] EMPTY_LIST
A ready-to-use empty list of Strings.

Method Detail

split

public static java.lang.String[] split(java.lang.String s)
Splits specified string at whitespace character boundaries. Returns list of parts.

Parameters:
s - string to be split
Returns:
list of parts

split

public static java.lang.String[] split(java.lang.String string,
                                       char separatorChar)
Splits String string at occurrences of char separatorChar.

Parameters:
string - the String to be split
separatorChar - the char where to split
Returns:
the list of substrings resulting from splitting String string at occurrences of char separatorChar.

Note that each occurrence of separatorChar specifies the end of a substring. Therefore, the returned list may contain empty substrings if consecutive separatorChars are found in String string.

However, for consistency with all the other split methods, this method returns an empty array for the empty string.


join

public static java.lang.String join(char separatorChar,
                                    java.lang.String... strings)
Equivalent to join(Character.toString(separatorChar), strings).


join

public static java.lang.String join(java.lang.String separator,
                                    java.lang.String... strings)
Joins the items of the specified list of Strings using specified separator.

Parameters:
separator - the string used to join items
strings - the list where items are to be joined
Returns:
a string where all list items have been joined

wordWrap

public static final java.lang.String[] wordWrap(java.lang.String text,
                                                int maxLineLength)
Splits specified string at word boundaries, returning an array of lines having at most specified number of characters.

Parameters:
text - string to be split at word boundaries
maxLineLength - the number of characters contained in a line should not exceed this value
Returns:
a (possibly empty) array of lines

splitArguments

public static java.lang.String[] splitArguments(java.lang.String string)
Splits specified string in a manner which is similar to what is done for command line arguments. Returns the list of ``command line arguments''.

Example: returns {"one", "two", "three", " \"four\" ", " 'five' "} for input string:

one   "two"    'three'   " \"four\" "   " 'five' "

Note that escaped sequences such as "\n" and "\t" contained in specified string are left as is. The reason for this is that specified string may contain any whitespace character including '\n' and '\t', provided that these characters are contained in a quoted argument.


joinArguments

public static java.lang.String joinArguments(java.lang.String... args)
Inverse operation of splitArguments(java.lang.String). Returns ``command line''.

See Also:
quoteArgument(java.lang.String)

quoteArgument

public static java.lang.String quoteArgument(java.lang.String arg)
Quotes specified string using '\"' if needed to. Returns quoted string.

Note that whitespace characters such as '\n' and '\t' are not escaped as "\n" and "\t". Instead, the whole string is quoted. This is sufficient to allow it to contain any whitespace character.

See Also:
joinArguments(java.lang.String...)

substituteVars

public static java.lang.String substituteVars(java.lang.String text,
                                              char[] names,
                                              java.lang.Object[] values)
Equivalent to substituteVars(text, names, values, null, null, false).


substituteVars

public static java.lang.String substituteVars(java.lang.String text,
                                              char[] names,
                                              java.lang.Object[] values,
                                              java.lang.String[] args,
                                              java.lang.String allArgs)
Equivalent to substituteVars(text, names, values, args, allArgs, false).


substituteVars

public static java.lang.String substituteVars(java.lang.String text,
                                              char[] names,
                                              java.lang.Object[] values,
                                              java.lang.String[] args,
                                              java.lang.String allArgs,
                                              boolean allowForeignVars)
Returns specified text where %0, %1, ..., %9, %* and %X, %Y, etc, variables have been subsituted by specified values.

"%%" may be used to escape character "%".

A variable, whether named or not, %X may also be specified as %{X} etc.

Parameters:
text - text containing variables
names - the one-character long name of named variables %X, %Y, etc.

May be null.

values - the values of named variables %X, %Y, etc.

These objects are converted to strings using the toString() method.

When there are no enough values, the corresponding variables are not replaced.

May be null if names is also null.

args - the values of %0, %1, ..., %9 variables.

When there are no enough values, the corresponding variables are replaced by the empty string.

May be null.

allArgs - the values of %* variable.

May be null, in which case, args are joined using joinArguments(java.lang.String...) to form allArgs.

allowForeignVars - if true, unknown variables specified as %{var_name} are assumed to be system properties or environment variables and are subsituted as such.
Returns:
specified text where variables have been subsituted with their values

replaceAll

public static java.lang.String replaceAll(java.lang.String string,
                                          java.lang.String oldSub,
                                          java.lang.String newSub)
Replaces substring oldSub by substring newSub inside String string.

Parameters:
string - the String where replacements are to be performed
oldSub - the substring to replace
newSub - the replacement substring
Returns:
a string where all replacements have been performed
See Also:
String.replace(char, char)

capitalize

public static java.lang.String capitalize(java.lang.String string)
Returns the specified string with its first character converted to upper case.

Parameters:
string - the String to be processed
Returns:
the specified string with its first character converted to upper case

uncapitalize

public static java.lang.String uncapitalize(java.lang.String string)
Returns the specified string with its first character converted to lower case.

Parameters:
string - the String to be processed
Returns:
the specified string with its first character converted to lower case

quote

public static java.lang.String quote(java.lang.String string)
Like escape(java.lang.String) but puts a double quote character ('\"') around the escaped string.


escape

public static java.lang.String escape(java.lang.String string)
Returns the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences (that is '\n', 'é', etc).

Parameters:
string - the String to be escaped
Returns:
the specified string with all non-ASCII characters and non-printable ASCII characters replaced by the corresponding Java escape sequences

escape

public static void escape(java.lang.String string,
                          java.lang.StringBuilder buffer)
Same as escape(String) except that the escaped string is appended to specified buffer.


escape

public static java.lang.String escape(char c)
Returns the \\uXXXX Java escape sequence corresponding to specified character.

Parameters:
c - the character to be escaped
Returns:
A \\uXXXX Java escape sequence

escape

public static void escape(char c,
                          java.lang.StringBuilder buffer)
Same as escape(char) expect that the \\uXXXX Java escape sequence is appended to specified buffer.


unquote

public static java.lang.String unquote(java.lang.String string)
Like unescape(java.lang.String) but removes the double quote characters ('\"'), if any, before unescaping the string.


unescape

public static java.lang.String unescape(java.lang.String string)
Returns the specified string with Java escape sequences (that is '\n', 'é', etc) replaced by the corresponding character.

Parameters:
string - the String to be unescaped
Returns:
the specified string with Java escape sequences replaced by the corresponding character