org.crosswire.common.util
Class StringUtil

java.lang.Object
  extended by org.crosswire.common.util.StringUtil

public final class StringUtil
extends Object

A generic class of String utilities.

Author:
Joe Walker
See Also:
The GNU Lesser General Public License for details.

Field Summary
static String[] EMPTY_STRING_ARRAY
          An empty immutable String array.
static String NEWLINE
          The newline character
 
Constructor Summary
private StringUtil()
          Prevent instantiation
 
Method Summary
static String createTitle(String variable)
          This function creates a readable title from a variable name type input.
static String getInitials(String sentence)
          For example getInitials("Java DataBase Connectivity") = "JDC" and getInitials("Church of England") = "CoE".
static int indexOfAny(char[] str, char[] separatorChars, int offset)
          Find the first occurrence of a separator in the character buffer beginning at the given offset.
static int indexOfWhitespace(char[] str, int offset)
          Find the first occurrence of a whitespace in the character buffer beginning at the given offset.
static String join(Object[] array, String aSeparator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static String read(Reader in)
          This method reads an InputStream In its entirety, and passes The text back as a string.
static String[] split(String str)
          Splits the provided text into an array, using whitespace as the separator.
static String[] split(String str, char separatorChar)
          Splits the provided text into an array, separator specified.
static String[] split(String str, char separatorChar, int max)
           Splits the provided text into an array, separator specified.
static String[] split(String str, int max)
          Splits the provided text into an array, using whitespace as the separator.
static String[] split(String str, String separatorChars)
           Splits the provided text into an array, separators specified.
static String[] split(String str, String separatorStr, int max)
           Splits the provided text into an array, separators specified.
static String[] splitAll(String str, char separatorChar)
           Splits the provided text into an array, separator specified.
static String[] splitAll(String str, char separatorChar, int max)
           Splits the provided text into an array, separator specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEWLINE

public static final String NEWLINE
The newline character


EMPTY_STRING_ARRAY

public static final String[] EMPTY_STRING_ARRAY
An empty immutable String array.

Constructor Detail

StringUtil

private StringUtil()
Prevent instantiation

Method Detail

read

public static String read(Reader in)
                   throws IOException
This method reads an InputStream In its entirety, and passes The text back as a string. If you are reading from a source that can block then be prepared for a long wait for this to return.

Parameters:
in - The Stream to read from.
Returns:
A string containing all the text from the Stream.
Throws:
IOException - when an I/O error occurred

createTitle

public static String createTitle(String variable)
This function creates a readable title from a variable name type input. For example calling: StringUtil.createTitle("one_two") = "One Two" StringUtil.createTitle("oneTwo") = "One Two"

Parameters:
variable - the name of a variable
Returns:
the generated title

getInitials

public static String getInitials(String sentence)
For example getInitials("Java DataBase Connectivity") = "JDC" and getInitials("Church of England") = "CoE".

Parameters:
sentence - The phrase from which to get the initial letters.
Returns:
The initial letters in the given words.

split

public static String[] split(String str)
Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

 StringUtil.split(null)       = []
 StringUtil.split("")         = []
 StringUtil.split("abc def")  = ["abc", "def"]
 StringUtil.split("abc  def") = ["abc", "def"]
 StringUtil.split(" abc ")    = ["abc"]
 

Parameters:
str - the String to parse, may be null
Returns:
an array of parsed Strings, null if null String input

split

public static String[] split(String str,
                             int max)
Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

 StringUtil.split(null)       = []
 StringUtil.split("")         = []
 StringUtil.split("abc def")  = ["abc", "def"]
 StringUtil.split("abc  def") = ["abc", "def"]
 StringUtil.split(" abc ")    = ["abc"]
 

Parameters:
str - the String to parse, may be null
max - the maximum number of elements to return
Returns:
an array of parsed Strings, null if null String input

split

public static String[] split(String str,
                             char separatorChar)
Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null.

 StringUtil.split(null, *)         = []
 StringUtil.split("", *)           = []
 StringUtil.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtil.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtil.split("a:b:c", '.')    = ["a:b:c"]
 StringUtil.split("a b c", ' ')    = ["a", "b", "c"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
Returns:
an array of parsed Strings

split

public static String[] split(String str,
                             char separatorChar,
                             int max)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null.

 StringUtil.split(null, *, 2)         = []
 StringUtil.split("", *, 2)           = []
 StringUtil.split("a.b.c", '.', 2)    = ["a", "b"]
 StringUtil.split("a..b.c", '.', 2)   = ["a", "b"]
 StringUtil.split("a:b:c", '.', 2)    = ["a:b:c"]
 StringUtil.split("a b c", ' ', 2)    = ["a", "b"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings

splitAll

public static String[] splitAll(String str,
                                char separatorChar)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated individually.

A null input String returns null.

 StringUtil.splitAll(null, *)         = []
 StringUtil.splitAll("", *)           = []
 StringUtil.splitAll("a.b.c", '.')    = ["a", "b", "c"]
 StringUtil.splitAll("a..b.c", '.')   = ["a", "", "b", "c"]
 StringUtil.splitAll("a:b:c", '.')    = ["a:b:c"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
Returns:
an array of parsed Strings

splitAll

public static String[] splitAll(String str,
                                char separatorChar,
                                int max)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated individually.

A null input String returns null.

 StringUtil.splitAll(null, *, 2)         = []
 StringUtil.splitAll("", *, 2)           = []
 StringUtil.splitAll("a.b.c", '.', 2)    = ["a", "b"]
 StringUtil.splitAll("a..b.c", '.', 2)   = ["a", ""]
 StringUtil.splitAll("a:b:c", '.', 2)    = ["a:b:c"]
 StringUtil.splitAll("a b c", ' ', 2)    = ["a", "b"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings

split

public static String[] split(String str,
                             String separatorChars)

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null. A null separatorChars splits on whitespace.

 StringUtil.split(null, *)         = []
 StringUtil.split("", *)           = []
 StringUtil.split("abc def", null) = ["abc", "def"]
 StringUtil.split("abc def", " ")  = ["abc", "def"]
 StringUtil.split("abc  def", " ") = ["abc", "def"]
 StringUtil.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
 

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
Returns:
an array of parsed Strings, null if null String input

split

public static String[] split(String str,
                             String separatorStr,
                             int max)

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null. A null separatorChars splits on whitespace.

 StringUtil.split(null, *, *)            = []
 StringUtil.split("", *, *)              = []
 StringUtil.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
 StringUtil.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
 StringUtil.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
 StringUtil.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
 

Parameters:
str - the String to parse, may be null
separatorStr - the characters used as the delimiters, null splits on whitespace
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings

join

public static String join(Object[] array,
                          String aSeparator)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

 StringUtil.join(null, *)                = null
 StringUtil.join([], *)                  = ""
 StringUtil.join([null], *)              = ""
 StringUtil.join(["a", "b", "c"], "--")  = "a--b--c"
 StringUtil.join(["a", "b", "c"], null)  = "abc"
 StringUtil.join(["a", "b", "c"], "")    = "abc"
 StringUtil.join([null, "", "a"], ',')   = ",,a"
 

Parameters:
array - the array of values to join together, may be null
aSeparator - the separator character to use, null treated as ""
Returns:
the joined String, null if null array input

indexOfAny

public static int indexOfAny(char[] str,
                             char[] separatorChars,
                             int offset)
Find the first occurrence of a separator in the character buffer beginning at the given offset.

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
offset - the index of the first character to consider
Returns:
the index of a separator char in the string or -1

indexOfWhitespace

public static int indexOfWhitespace(char[] str,
                                    int offset)
Find the first occurrence of a whitespace in the character buffer beginning at the given offset. Whitespace is defined by Character.isWhitespace(char).

Parameters:
str - the String to parse, may be null
offset - the index of the first character to consider
Returns:
the index of a separator char in the string or -1

Copyright ยจ 2003-2015