public final class StringUtil extends Object
The GNU Lesser General Public License for details.
Modifier and Type | Field and Description |
---|---|
static String[] |
EMPTY_STRING_ARRAY
An empty immutable
String array. |
static String |
NEWLINE
The newline character
|
Modifier | Constructor and Description |
---|---|
private |
StringUtil()
Prevent instantiation
|
Modifier and Type | Method and Description |
---|---|
static String |
createTitle(String variable)
This function creates a readable title from a variable name type input.
|
static boolean |
equals(String string1,
String string2)
Compare two strings for equality such that both can be null.
|
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.
|
public static final String NEWLINE
public static final String[] EMPTY_STRING_ARRAY
String
array.public static boolean equals(String string1, String string2)
string1
- the first stringstring2
- the second stringpublic static String read(Reader in) throws IOException
in
- The Stream to read from.IOException
- when an I/O error occurredpublic static String createTitle(String variable)
variable
- the name of a variablepublic static String getInitials(String sentence)
sentence
- The phrase from which to get the initial letters.public static String[] split(String str)
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"]
str
- the String to parse, may be nullnull
if null String
inputpublic static String[] split(String str, int max)
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"]
str
- the String to parse, may be nullmax
- the maximum number of elements to returnnull
if null String
inputpublic static String[] split(String str, char separatorChar)
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"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimiterpublic 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"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimitermax
- the maximum number of elements to include in the array.
A zero or negative value implies no limitpublic 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"]
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters, null
splits on whitespacenull
if null String
inputpublic 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"]
str
- the String to parse, may be nullseparatorStr
- the characters used as the delimiters, null
splits on whitespacemax
- the maximum number of elements to include in the array. A zero
or negative value implies no limitpublic 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"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimiterpublic 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"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimitermax
- the maximum number of elements to include in the array.
A zero or negative value implies no limitpublic 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"
array
- the array of values to join together, may be nullaSeparator
- the separator character to use, null treated as ""null
if null array inputpublic static int indexOfAny(char[] str, char[] separatorChars, int offset)
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters, null
splits on whitespaceoffset
- the index of the first character to considerpublic static int indexOfWhitespace(char[] str, int offset)
Character.isWhitespace(char)
.str
- the String to parse, may be nulloffset
- the index of the first character to consider