|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.drools.util.StringUtils
public class StringUtils
Ripped form commons StringUtil:
Operations on String that are
null safe.
The StringUtils class defines certain words related to
String handling.
null"")' ', char 32)Character.isWhitespace(char)String.trim()StringUtils handles null input Strings quietly.
That is to say that a null input will return null.
Where a boolean or int is being returned
details vary by method.
A side effect of the null handling is that a
NullPointerException should be considered a bug in
StringUtils (except for deprecated methods).
Methods in this class give sample code to explain their operation.
The symbol * is used to indicate any input including null.
String| Field Summary | |
|---|---|
static String |
EMPTY
The empty String "". |
static String[] |
EMPTY_STRING_ARRAY
An empty immutable String array. |
static int |
INDEX_NOT_FOUND
Represents a failed index search. |
| Constructor Summary | |
|---|---|
StringUtils()
StringUtils instances should NOT be constructed in
standard programming. |
|
| Method Summary | |
|---|---|
static boolean |
isEmpty(String str)
Checks if a String is empty ("") or null. |
static String |
padding(int repeat,
char padChar)
Returns padding using the specified delimiter repeated to a given length. |
static String |
readFileAsString(Reader reader)
|
static String |
repeat(String str,
int repeat)
Repeat a String repeat times to form a
new 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 with a maximum length, separators specified. |
static String[] |
splitPreserveAllTokens(String str)
Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators. |
static String[] |
splitPreserveAllTokens(String str,
char separatorChar)
Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. |
static String[] |
splitPreserveAllTokens(String str,
String separatorChars)
Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. |
static String[] |
splitPreserveAllTokens(String str,
String separatorChars,
int max)
Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators. |
static String |
ucFirst(String name)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String[] EMPTY_STRING_ARRAY
String array.
public static final String EMPTY
"".
public static final int INDEX_NOT_FOUND
| Constructor Detail |
|---|
public StringUtils()
StringUtils instances should NOT be constructed in
standard programming. Instead, the class should be used as
StringUtils.trim(" foo ");.
This constructor is public to permit tools that require a JavaBean instance to operate.
| Method Detail |
|---|
public static String ucFirst(String name)
public static boolean isEmpty(String str)
Checks if a String is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
str - the String to check, may be null
true if the String is empty or nullpublic static String repeat(String str, int repeat)
Repeat a String repeat times to form a
new String.
StringUtils.repeat(null, 2) = null
StringUtils.repeat("", 0) = ""
StringUtils.repeat("", 2) = ""
StringUtils.repeat("a", 3) = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""
str - the String to repeat, may be nullrepeat - number of times to repeat str, negative treated as zero
null if null String inputpublic 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. For more control over the split use the StrTokenizer class.
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"]
str - the String to parse, may be null
null if null String inputpublic 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. For more control over the split use the StrTokenizer class.
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"]
str - the String to parse, may be nullseparatorChar - the character used as the delimiter,
null splits on whitespace
null if null String inputpublic 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. For more control over the split use the StrTokenizer class.
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"]
str - the String to parse, may be nullseparatorChars - the characters used as the delimiters,
null splits on whitespace
null if null String inputpublic static String[] split(String str, String separatorChars, int max)
Splits the provided text into an array with a maximum length, separators specified.
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.
If more than max delimited substrings are found, the last
returned string includes all characters after the first max - 1
returned strings (including separator characters).
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", "cd:ef"]
str - the String to parse, may be nullseparatorChars - 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 limit
null if null String inputpublic static String[] splitPreserveAllTokens(String str)
Splits the provided text into an array, using whitespace as the
separator, preserving all tokens, including empty tokens created by
adjacent separators. This is an alternative to using StringTokenizer.
Whitespace is defined by Character.isWhitespace(char).
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null input String returns null.
StringUtils.splitPreserveAllTokens(null) = null
StringUtils.splitPreserveAllTokens("") = []
StringUtils.splitPreserveAllTokens("abc def") = ["abc", "def"]
StringUtils.splitPreserveAllTokens("abc def") = ["abc", "", "def"]
StringUtils.splitPreserveAllTokens(" abc ") = ["", "abc", ""]
str - the String to parse, may be null
null if null String inputpublic static String[] splitPreserveAllTokens(String str, char separatorChar)
Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null input String returns null.
StringUtils.splitPreserveAllTokens(null, *) = null
StringUtils.splitPreserveAllTokens("", *) = []
StringUtils.splitPreserveAllTokens("a.b.c", '.') = ["a", "b", "c"]
StringUtils.splitPreserveAllTokens("a..b.c", '.') = ["a", "", "b", "c"]
StringUtils.splitPreserveAllTokens("a:b:c", '.') = ["a:b:c"]
StringUtils.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"]
StringUtils.splitPreserveAllTokens("a b c", ' ') = ["a", "b", "c"]
StringUtils.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", ""]
StringUtils.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", "", ""]
StringUtils.splitPreserveAllTokens(" a b c", ' ') = ["", a", "b", "c"]
StringUtils.splitPreserveAllTokens(" a b c", ' ') = ["", "", a", "b", "c"]
StringUtils.splitPreserveAllTokens(" a b c ", ' ') = ["", a", "b", "c", ""]
str - the String to parse, may be nullseparatorChar - the character used as the delimiter,
null splits on whitespace
null if null String inputpublic static String[] splitPreserveAllTokens(String str, String separatorChars)
Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null input String returns null.
A null separatorChars splits on whitespace.
StringUtils.splitPreserveAllTokens(null, *) = null
StringUtils.splitPreserveAllTokens("", *) = []
StringUtils.splitPreserveAllTokens("abc def", null) = ["abc", "def"]
StringUtils.splitPreserveAllTokens("abc def", " ") = ["abc", "def"]
StringUtils.splitPreserveAllTokens("abc def", " ") = ["abc", "", def"]
StringUtils.splitPreserveAllTokens("ab:cd:ef", ":") = ["ab", "cd", "ef"]
StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":") = ["ab", "cd", "ef", ""]
StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
StringUtils.splitPreserveAllTokens("ab::cd:ef", ":") = ["ab", "", cd", "ef"]
StringUtils.splitPreserveAllTokens(":cd:ef", ":") = ["", cd", "ef"]
StringUtils.splitPreserveAllTokens("::cd:ef", ":") = ["", "", cd", "ef"]
StringUtils.splitPreserveAllTokens(":cd:ef:", ":") = ["", cd", "ef", ""]
str - the String to parse, may be nullseparatorChars - the characters used as the delimiters,
null splits on whitespace
null if null String inputpublic static String[] splitPreserveAllTokens(String str, String separatorChars, int max)
Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. Adjacent separators are treated as one separator.
A null input String returns null.
A null separatorChars splits on whitespace.
If more than max delimited substrings are found, the last
returned string includes all characters after the first max - 1
returned strings (including separator characters).
StringUtils.splitPreserveAllTokens(null, *, *) = null
StringUtils.splitPreserveAllTokens("", *, *) = []
StringUtils.splitPreserveAllTokens("ab de fg", null, 0) = ["ab", "cd", "ef"]
StringUtils.splitPreserveAllTokens("ab de fg", null, 0) = ["ab", "cd", "ef"]
StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
StringUtils.splitPreserveAllTokens("ab de fg", null, 2) = ["ab", " de fg"]
StringUtils.splitPreserveAllTokens("ab de fg", null, 3) = ["ab", "", " de fg"]
StringUtils.splitPreserveAllTokens("ab de fg", null, 4) = ["ab", "", "", "de fg"]
str - the String to parse, may be nullseparatorChars - 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 limit
null if null String inputpublic static String padding(int repeat, char padChar) throws IndexOutOfBoundsException
Returns padding using the specified delimiter repeated to a given length.
StringUtils.padding(0, 'e') = "" StringUtils.padding(3, 'e') = "eee" StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
Note: this method doesn't not support padding with
Unicode Supplementary Characters
as they require a pair of chars to be represented.
If you are needing to support full I18N of your applications
consider using repeat(String, int) instead.
repeat - number of times to repeat delimpadChar - character to repeat
IndexOutOfBoundsException - if repeat < 0repeat(String, int)public static String readFileAsString(Reader reader)
filePath - the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||