public class StringUtils extends Object
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
Modifier and Type | Class and Description |
---|---|
static class |
StringUtils.SIMILARITY_STRATS |
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
StringUtils()
StringUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static String |
cleanPath(String path)
Normalize the path by suppressing sequences like "path/.." and
inner simple dots.
|
static boolean |
codeAwareEqualsIgnoreSpaces(String in1,
String in2)
Compares two string being equals ignoring whitespaces, but preserving whitespace between double-quotes
The two inputs MUST BE valid DRL/Java syntax (this validation is NOT performed by this method, this method assumes they are).
|
static String |
collectionToDelimitedString(Collection coll,
String delim)
Convenience method to return a Collection as a delimited (e.g.
|
static String |
collectionToDelimitedString(Collection coll,
String delim,
String prefix,
String suffix)
Convenience method to return a Collection as a delimited (e.g.
|
static String |
deleteAny(String inString,
String charsToDelete)
Delete any character in a given String.
|
static String[] |
delimitedListToStringArray(String str,
String delimiter)
Take a String which is a delimited list and convert it to a String array.
|
static String[] |
delimitedListToStringArray(String str,
String delimiter,
String charsToDelete)
Take a String which is a delimited list and convert it to a String array.
|
static boolean |
equalsIgnoreSpaces(String s1,
String s2) |
static String |
escapeXmlString(String string) |
static String |
extractFirstIdentifier(String string,
int start) |
static int |
extractFirstIdentifier(String string,
StringBuilder builder,
int start) |
static int |
findEndOfMethodArgsIndex(CharSequence string,
int startOfMethodArgsIndex) |
static String |
generateUUID() |
static int |
indexOfOutOfQuotes(String str,
char searched) |
static int |
indexOfOutOfQuotes(String str,
String searched) |
static boolean |
isDereferencingIdentifier(String expr) |
static boolean |
isEmpty(CharSequence str)
Checks if a String is empty ("") or null.
|
static boolean |
isIdentifier(String expr) |
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 |
replace(String inString,
String oldPattern,
String newPattern)
Replace all occurences of a substring within a string with
another string.
|
static int |
skipBlanks(String string,
int start) |
static List<String> |
splitArgumentsList(CharSequence string) |
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 List<String> |
splitStatements(CharSequence string) |
static double |
stringSimilarity(String s1,
String s2,
StringUtils.SIMILARITY_STRATS method) |
static String |
toString(BufferedReader reader) |
static String |
toString(InputStream is) |
static String |
toString(Reader reader) |
static String[] |
toStringArray(Collection collection)
Copy the given Collection into a String array.
|
static URI |
toURI(String location) |
static String |
ucFirst(String name) |
static String |
unescapeJava(String str)
Unescapes any Java literals found in the
String . |
static void |
unescapeJava(Writer out,
String str)
Unescapes any Java literals found in the
String to a
Writer . |
public static final String[] EMPTY_STRING_ARRAY
String
array.public static final String EMPTY
""
.public static final int INDEX_NOT_FOUND
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.
public static boolean isEmpty(CharSequence 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 nulltrue
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 zeronull
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 null
separatorChars
- the characters used as the delimiters,
null
splits on whitespacenull
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 char
s 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 repeatIndexOutOfBoundsException
- if repeat < 0
repeat(String, int)
public static String unescapeJava(String str)
Unescapes any Java literals found in the String
.
For example, it will turn a sequence of '\'
and
'n'
into a newline character, unless the '\'
is preceded by another '\'
.
str
- the String
to unescape, may be nullString
, null
if null string inputpublic static void unescapeJava(Writer out, String str) throws IOException
Unescapes any Java literals found in the String
to a
Writer
.
For example, it will turn a sequence of '\'
and
'n'
into a newline character, unless the '\'
is preceded by another '\'
.
A null
string input has no effect.
out
- the Writer
used to output unescaped charactersstr
- the String
to unescape, may be nullIllegalArgumentException
- if the Writer is null
IOException
- if error occurs on underlying Writerpublic static String cleanPath(String path)
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
path
- the original pathpublic static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
toString()
implementations.coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")prefix
- the String to start each element withsuffix
- the String to end each element withpublic static String collectionToDelimitedString(Collection coll, String delim)
toString()
implementations.coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")public static String replace(String inString, String oldPattern, String newPattern)
inString
- String to examineoldPattern
- String to replacenewPattern
- String to insertpublic static URI toURI(String location) throws URISyntaxException
URISyntaxException
public static String[] delimitedListToStringArray(String str, String delimiter)
A single delimiter can consists of more than one character: It will still
be considered as single delimiter string, rather than as bunch of potential
delimiter characters - in contrast to tokenizeToStringArray
.
str
- the input Stringdelimiter
- the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
A single delimiter can consists of more than one character: It will still
be considered as single delimiter string, rather than as bunch of potential
delimiter characters - in contrast to tokenizeToStringArray
.
str
- the input Stringdelimiter
- the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)charsToDelete
- a set of characters to delete. Useful for deleting unwanted
line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String.public static String[] toStringArray(Collection collection)
collection
- the Collection to copynull
if the passed-in
Collection was null
)
Borrowed from Spring, under the ASL2.0 license.public static String deleteAny(String inString, String charsToDelete)
inString
- the original StringcharsToDelete
- a set of characters to delete.
E.g. "az\n" will delete 'a's, 'z's and new lines.public static String toString(Reader reader) throws IOException
IOException
public static String toString(InputStream is) throws IOException
IOException
public static String toString(BufferedReader reader) throws IOException
IOException
public static String generateUUID()
public static int extractFirstIdentifier(String string, StringBuilder builder, int start)
public static int skipBlanks(String string, int start)
public static List<String> splitStatements(CharSequence string)
public static List<String> splitArgumentsList(CharSequence string)
public static boolean codeAwareEqualsIgnoreSpaces(String in1, String in2)
public static int findEndOfMethodArgsIndex(CharSequence string, int startOfMethodArgsIndex)
public static int indexOfOutOfQuotes(String str, char searched)
public static boolean isIdentifier(String expr)
public static boolean isDereferencingIdentifier(String expr)
public static double stringSimilarity(String s1, String s2, StringUtils.SIMILARITY_STRATS method)
Copyright © 2001–2017 JBoss by Red Hat. All rights reserved.