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 utils. It would be good if we could put this stuff in java.lang ...

Author:
Joe Walker [joe at eireneh dot com]
See Also:
for license details.
The copyright to this program is held by it's authors.

Field Summary
static String[] EMPTY_STRING_ARRAY
          An empty immutable String array.
static String NEWLINE
          The newline character
 
Constructor Summary
private StringUtil()
          Prevent Instansiation
 
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 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, String separatorChars)
          Splits the provided text into an array, separators specified.
static String[] split(String str, String separatorChars, 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.
 
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 Instansiation

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 preapred 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

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"


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.

A null input String returns null.

 StringUtils.split(null)       = null
 StringUtils.split("")         = []
 StringUtils.split("abc def")  = ["abc", "def"]
 StringUtils.split("abc  def") = ["abc", "def"]
 StringUtils.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,
                             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.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
 StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 

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

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.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
 StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 

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

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.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("abc def", null) = ["abc", "def"]
 StringUtils.split("abc def", " ")  = ["abc", "def"]
 StringUtils.split("abc  def", " ") = ["abc", "def"]
 StringUtils.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 separatorChars,
                             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.

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

Parameters:
str - the String to parse, may be null
separatorChars - 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.

 StringUtils.join(null, *)                = null
 StringUtils.join([], *)                  = ""
 StringUtils.join([null], *)              = ""
 StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
 StringUtils.join(["a", "b", "c"], null)  = "abc"
 StringUtils.join(["a", "b", "c"], "")    = "abc"
 StringUtils.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

Copyright ? 2003-2006