public class StringUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
EMPTY_STRING
The Constant EMPTY_STRING.
|
| 构造器和说明 |
|---|
StringUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
escapeJava(String str)
Escapes the characters in a
String using Java String rules. |
static void |
escapeJava(Writer out,
String str)
Escapes the characters in a
String using Java String rules to a Writer. |
static boolean |
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isEmpty(String str)
Checks if a String is empty ("") or null.
|
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[] |
splitByWholeSeparator(String str,
String separator)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparator(String str,
String separator,
int max)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparatorPreserveAllTokens(String str,
String separator)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparatorPreserveAllTokens(String str,
String separator,
int max)
Splits the provided text into an array, separator string specified.
|
static int |
stringToInt(String str,
int defaultValue)
已过时。
Use
toInt(String, int) This method will be removed in Commons Lang 3.0 |
static int |
toInt(String str)
Convert a
String to an int, returning zero if the conversion fails. |
static int |
toInt(String str,
int defaultValue)
Convert a
String to an int, returning a default value if the conversion fails. |
static long |
toLong(String str)
Convert a
String to an long, returning a default value if the conversion fails. |
static long |
toLong(String str,
long defaultValue)
Convert a
String to an long, returning a default value if the conversion fails. |
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 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 nulltrue if the String is empty or nullpublic static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
str - the String to check, may be nulltrue if the String is null, empty or whitespacepublic static int toInt(String str)
Convert a String to an int, returning zero if the conversion fails.
If the string is null, zero is returned.
NumberUtils.toInt(null) = 0
NumberUtils.toInt("") = 0
NumberUtils.toInt("1") = 1
str - the string to convert, may be nullzero if conversion failspublic static int stringToInt(String str, int defaultValue)
toInt(String, int) This method will be removed in Commons Lang 3.0
Convert a String to an int, returning a default value if the conversion fails.
If the string is null, the default value is returned.
NumberUtils.stringToInt(null, 1) = 1
NumberUtils.stringToInt("", 1) = 1
NumberUtils.stringToInt("1", 0) = 1
str - the string to convert, may be nulldefaultValue - the default valuepublic static int toInt(String str, int defaultValue)
Convert a String to an int, returning a default value if the conversion fails.
If the string is null, the default value is returned.
NumberUtils.toInt(null, 1) = 1
NumberUtils.toInt("", 1) = 1
NumberUtils.toInt("1", 0) = 1
str - the string to convert, may be nulldefaultValue - the default valuepublic static long toLong(String str)
Convert a String to an long, returning a default value if the conversion fails.
If the string is null, the default value is returned.
NumberUtils.toLong(null) = 0
NumberUtils.toLong("") = 0
NumberUtils.toLong("1") = 1
str - the string to convert, may be nullpublic static long toLong(String str, long defaultValue)
Convert a String to an long, returning a default value if the conversion fails.
If the string is null, the default value is returned.
NumberUtils.toLong(null, 1) = 1
NumberUtils.toLong("", 1) = 1
NumberUtils.toLong("1", 0) = 1
str - the string to convert, may be nulldefaultValue - the default valuepublic static String escapeJava(String str)
Escapes the characters in a String using Java String rules.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
So a tab becomes the characters '\\' and 't'.
The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.
Example:
input string: He didn't say, "Stop!" output string: He didn't say, \"Stop!\"
str - String to escape values in, may be nullnull if null string inputpublic static void escapeJava(Writer out, String str) throws IOException
Escapes the characters in a String using Java String rules to a Writer.
A null string input has no effect.
out - Writer to write escaped string intostr - String to escape values in, may be nullIOException - if error occurs on underlying WriterIllegalArgumentException - if the Writer is nullescapeJava(java.lang.String)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 nullIOException - if error occurs on underlying WriterIllegalArgumentException - if the Writer is nullpublic 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 nullnull 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 b c", ' ') = ["a", "b", "c"]
str - the String to parse, may be nullseparatorChar - the character used as the delimiternull 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 whitespacenull 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 limitnull if null String inputpublic static String[] splitByWholeSeparator(String str, String separator)
Splits the provided text into an array, separator string specified.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null input String returns null. A null separator splits on whitespace.
StringUtils.splitByWholeSeparator(null, *) = null
StringUtils.splitByWholeSeparator("", *) = []
StringUtils.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
str - the String to parse, may be nullseparator - String containing the String to be used as a delimiter, null splits on whitespacenull if null String was inputpublic static String[] splitByWholeSeparator(String str, String separator, int max)
Splits the provided text into an array, separator string specified. Returns a maximum of max
substrings.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null input String returns null. A null separator splits on whitespace.
StringUtils.splitByWholeSeparator(null, *, *) = null
StringUtils.splitByWholeSeparator("", *, *) = []
StringUtils.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparator("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
str - the String to parse, may be nullseparator - String containing the String to be used as a delimiter, null splits on whitespacemax - the maximum number of elements to include in the returned array. A zero or negative value implies no
limit.null if null String was inputpublic static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator)
Splits the provided text into an array, separator string specified.
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 separator splits on whitespace.
StringUtils.splitByWholeSeparatorPreserveAllTokens(null, *) = null
StringUtils.splitByWholeSeparatorPreserveAllTokens("", *) = []
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null) = ["ab", "", "", "de", "fg"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":") = ["ab", "cd", "ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
str - the String to parse, may be nullseparator - String containing the String to be used as a delimiter, null splits on whitespacenull if null String was inputpublic static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator, int max)
Splits the provided text into an array, separator string specified. Returns a maximum of max
substrings.
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 separator splits on whitespace.
StringUtils.splitByWholeSeparatorPreserveAllTokens(null, *, *) = null
StringUtils.splitByWholeSeparatorPreserveAllTokens("", *, *) = []
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 0) = ["ab", "de", "fg"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 0) = ["ab", "", "", "de", "fg"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
str - the String to parse, may be nullseparator - String containing the String to be used as a delimiter, null splits on whitespacemax - the maximum number of elements to include in the returned array. A zero or negative value implies no
limit.null if null String was inputCopyright © 2022 Baidu, Inc.. All rights reserved.