| java.lang.Object org.drools.util.StringUtils
StringUtils | public class StringUtils (Code) | | Ripped form commons StringUtil:
Operations on
java.lang.String that are
null safe.
- IsEmpty/IsBlank
- checks if a String contains text
- Trim/Strip
- removes leading and trailing whitespace
- Equals
- compares two strings null-safe
- IndexOf/LastIndexOf/Contains
- null-safe index-of checks
- IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut
- index-of any of a set of Strings
- ContainsOnly/ContainsNone
- does String contains only/none of these characters
- Substring/Left/Right/Mid
- null-safe substring extractions
- SubstringBefore/SubstringAfter/SubstringBetween
- substring extraction relative to other strings
- Split/Join
- splits a String into an array of substrings and vice versa
- Remove/Delete
- removes part of a String
- Replace/Overlay
- Searches a String and replaces one String with another
- Chomp/Chop
- removes the last part of a String
- LeftPad/RightPad/Center/Repeat
- pads a String
- UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize
- changes the case of a String
- CountMatches
- counts the number of occurrences of one String in another
- IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable
- checks the characters in a String
- DefaultString
- protects against a null input String
- Reverse/ReverseDelimited
- reverses a String
- Abbreviate
- abbreviates a string using ellipsis
- Difference
- compares two Strings and reports on their differences
- LevensteinDistance
- the number of changes needed to change one String into another
The StringUtils class defines certain words related to
String handling.
- null -
null
- empty - a zero-length string (
"" )
- space - the space character (
' ' , char 32)
- whitespace - the characters defined by
Character.isWhitespace(char)
- trim - the characters <= 32 as in
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 .
See Also: java.lang.String author: Apache Jakarta Turbine author: Jon S. Stevens author: Daniel Rall author: Greg Coladonato author: Ed Korthof author: Rand McNeely author: Stephen Colebourne author: Fredrik Westermarck author: Holger Krauth author: Alexander Day Chaffee author: Henning P. Schmiedehausen author: Arun Mammen Thomas author: Gary Gregory author: Phil Steitz author: Al Chou author: Michael Davey author: Reuben Sivan author: Chris Hyzer since: 1.0 version: $Id$ |
Constructor Summary | |
public | StringUtils() StringUtils instances should NOT be constructed in
standard programming.
|
Method Summary | |
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. | public static String | padding(int repeat, char padChar) 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 char s to be represented.
If you are needing to support full I18N of your applications
consider using
StringUtils.repeat(String,int) instead. | public static String | readFileAsString(Reader reader) Parameters: filePath - the name of the file to open. | public static String | repeat(String str, int repeat) | public static String[] | split(String str) Splits the provided text into an array, using whitespace as the
separator. | public static String[] | split(String str, char separatorChar) Splits the provided text into an array, separator specified. | public static String[] | split(String str, String separatorChars) Splits the provided text into an array, separators specified. | public 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 min delimited substrings are found, the last
returned string includes all characters after the first min - 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace Parameters: min - the maximum number of elements to include in thearray. | public 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. | public 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. | public 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. | public 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 min delimited substrings are found, the last
returned string includes all characters after the first min - 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace Parameters: min - the maximum number of elements to include in thearray. | public static String | ucFirst(String name) |
EMPTY | final public static String EMPTY(Code) | | The empty String "" .
since: 2.0 |
EMPTY_STRING_ARRAY | final public static String[] EMPTY_STRING_ARRAY(Code) | | An empty immutable String array.
|
INDEX_NOT_FOUND | final public static int INDEX_NOT_FOUND(Code) | | Represents a failed index search.
since: 2.1 |
StringUtils | public StringUtils()(Code) | | 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.
|
isEmpty | public static boolean isEmpty(String str)(Code) | | 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().
Parameters: str - the String to check, may be null true if the String is empty or null |
readFileAsString | public static String readFileAsString(Reader reader)(Code) | | Parameters: 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 |
repeat | public static String repeat(String str, int repeat)(Code) | | 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) = ""
Parameters: str - the String to repeat, may be null Parameters: repeat - number of times to repeat str, negative treated as zero a new String consisting of the original String repeated,null if null String input |
split | public static String[] split(String str)(Code) | | 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"]
Parameters: str - the String to parse, may be null an array of parsed Strings, null if null String input |
split | public static String[] split(String str, char separatorChar)(Code) | | 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChar - the character used as the delimiter,null splits on whitespace an array of parsed Strings, null if null String input since: 2.0 |
split | public static String[] split(String str, String separatorChars)(Code) | | 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace an array of parsed Strings, null if null String input |
split | public static String[] split(String str, String separatorChars, int max)(Code) | | 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 min delimited substrings are found, the last
returned string includes all characters after the first min - 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace Parameters: min - the maximum number of elements to include in thearray. A zero or negative value implies no limit an array of parsed Strings, null if null String input |
splitPreserveAllTokens | public static String[] splitPreserveAllTokens(String str)(Code) | | 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", ""]
Parameters: str - the String to parse, may be null an array of parsed Strings, null if null String input since: 2.1 |
splitPreserveAllTokens | public static String[] splitPreserveAllTokens(String str, char separatorChar)(Code) | | 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", ""]
Parameters: str - the String to parse, may be null Parameters: separatorChar - the character used as the delimiter,null splits on whitespace an array of parsed Strings, null if null String input since: 2.1 |
splitPreserveAllTokens | public static String[] splitPreserveAllTokens(String str, String separatorChars)(Code) | | 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", ""]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace an array of parsed Strings, null if null String input since: 2.1 |
splitPreserveAllTokens | public static String[] splitPreserveAllTokens(String str, String separatorChars, int max)(Code) | | 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 min delimited substrings are found, the last
returned string includes all characters after the first min - 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"]
Parameters: str - the String to parse, may be null Parameters: separatorChars - the characters used as the delimiters,null splits on whitespace Parameters: min - the maximum number of elements to include in thearray. A zero or negative value implies no limit an array of parsed Strings, null if null String input since: 2.1 |
|
|