public class StringUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
__ISO_8859_1 |
static String |
__UTF16 |
static String |
__UTF8 |
static String |
EMPTY |
static String[] |
EMPTY_STRING_ARRAY |
static char[] |
lowercases |
| 构造器和说明 |
|---|
StringUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
append(StringBuilder buf,
byte b,
int base)
append hex digit
|
static void |
append(StringBuilder buf,
String s,
int offset,
int length)
Append substring to StringBuilder
|
static void |
append2digits(StringBuffer buf,
int i)
Append 2 digits (zero padded) to the StringBuffer
|
static void |
append2digits(StringBuilder buf,
int i)
Append 2 digits (zero padded) to the StringBuilder
|
static String |
applyRelativePath(String path,
String relativePath)
Apply the given relative path to the given Java resource path,
assuming standard Java folder separation (i.e. "/" separators).
|
static String |
asciiToLowerCase(String s)
fast lower case conversion.
|
static String |
cleanPath(String path)
Normalize the path by suppressing sequences like "path/.." and
inner simple dots.
|
static String |
collectionToCommaDelimitedString(Collection<?> coll)
Convert a
Collection into a delimited String (e.g., CSV). |
static String |
collectionToDelimitedString(Collection<?> coll,
String delim)
Convert a
Collection into a delimited String (e.g. |
static String |
collectionToDelimitedString(Collection<?> coll,
String delim,
String prefix,
String suffix)
Convert a
Collection to a delimited String (e.g. |
static List<String> |
csvSplit(List<String> list,
String s,
int off,
int len)
Split a quoted comma separated string to a list
Handle rfc4180-like
CSV strings, with the exceptions:
quoted values may contain double quotes escaped with back-slash
Non-quoted values are trimmed of leading trailing white space
trailing commas are ignored
double commas result in a empty string value
|
static String[] |
csvSplit(String s)
Parse a CSV string using
csvSplit(List,String, int, int) |
static String[] |
csvSplit(String s,
int off,
int len)
Parse a CSV string using
csvSplit(List,String, int, int) |
static String |
deleteAny(String inString,
String charsToDelete)
Delete any character in a given
String. |
static String[] |
delimitedListToStringArray(String str,
String delimiter)
Take a
String that is a delimited list and convert it into a
String array. |
static String[] |
delimitedListToStringArray(String str,
String delimiter,
String charsToDelete)
Take a
String that is a delimited list and convert it into
a String array. |
static String |
escapeXML(String str) |
static byte[] |
getBytes(String s) |
static byte[] |
getBytes(String s,
String charset) |
static String |
getFilename(String path)
Extract the filename from the given Java resource path,
e.g.
|
static String |
getFilenameExtension(String path)
Extract the filename extension from the given Java resource path,
e.g.
|
static byte[] |
getUtf8Bytes(String s) |
static boolean |
hasLength(CharSequence str) |
static boolean |
hasLength(String str) |
static boolean |
hasText(CharSequence str) |
static boolean |
hasText(String str) |
static boolean |
isAlpha(String value) |
static String |
normalizeCharset(String s)
Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
|
static String |
normalizeCharset(String s,
int offset,
int length)
Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
|
static String |
replace(String s,
Map<String,Object> map)
Replace the pattern using a map, such as a pattern, such as A pattern is
"hello ${foo}" and the map is {"foo" : "world"}, when you execute this
function, the result is "hello world"
|
static String |
replace(String s,
Object... objs) |
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 boolean |
startsWithIgnoreCase(String str,
String prefix)
Test if the given
String starts with the specified prefix,
ignoring upper/lower case. |
static int |
toInt(String string,
int from)
Convert String to an integer.
|
static long |
toLong(String string)
Convert String to an long.
|
static String |
unicodeToString(String s)
Convert a string that is unicode form to a normal string.
|
public static final String[] EMPTY_STRING_ARRAY
public static final char[] lowercases
public static String normalizeCharset(String s)
s - the charset to normalizepublic static String normalizeCharset(String s, int offset, int length)
s - the charset to normalizeoffset - the offset in the charsetlength - the length of the charset in the input parampublic 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, 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, 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, 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 boolean hasText(String str)
public static boolean hasText(CharSequence str)
public static boolean hasLength(CharSequence str)
public static boolean hasLength(String str)
public static String replace(String s, Map<String,Object> map)
s - The pattern string.map - The key-valuepublic static String unicodeToString(String s)
s - The unicode form of a string, e.g. "\\u8001\\u9A6C"public static String asciiToLowerCase(String s)
s - the string to convertpublic static void append2digits(StringBuffer buf, int i)
buf - the buffer to append toi - the value to appendpublic static void append2digits(StringBuilder buf, int i)
buf - the buffer to append toi - the value to appendpublic static void append(StringBuilder buf, String s, int offset, int length)
buf - StringBuilder to append tos - String to append fromoffset - The offset of the substringlength - The length of the substringpublic static void append(StringBuilder buf, byte b, int base)
buf - the buffer to append tob - the byte to appendbase - the base of the hex output (almost always 16).public static int toInt(String string, int from)
string - A String containing an integer.from - The index to start parsing frompublic static long toLong(String string)
string - A String containing an integer.public static byte[] getBytes(String s)
public static byte[] getUtf8Bytes(String s)
public static String[] csvSplit(String s)
csvSplit(List,String, int, int)s - The string to parsepublic static String[] csvSplit(String s, int off, int len)
csvSplit(List,String, int, int)s - The string to parseoff - The offset into the string to start parsinglen - The len in characters to parsepublic static List<String> csvSplit(List<String> list, String s, int off, int len)
Handle rfc4180-like CSV strings, with the exceptions:
list - The Collection to split to (or null to get a new list)s - The string to parseoff - The offset into the string to start parsinglen - The len in characters to parsepublic static final boolean isAlpha(String value)
public static boolean startsWithIgnoreCase(String str, String prefix)
String starts with the specified prefix,
ignoring upper/lower case.str - the String to checkprefix - the prefix to look forString.startsWith(java.lang.String, int)public static String applyRelativePath(String path, String relativePath)
path - the path to start from (usually a full file path)relativePath - the relative path to apply
(relative to the full file path above)public 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[] delimitedListToStringArray(String str, String delimiter)
String that is a delimited list and convert it into a
String array.
A single delimiter may consist of more than one character,
but it will still be considered as a 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)
String that is a delimited list and convert it into
a String array.
A single delimiter may consist of more than one character,
but it will still be considered as a 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 Stringpublic static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
coll - the Collection to convertdelim - the delimiter to use (typically a ",")prefix - the String to start each element withsuffix - the String to end each element withStringpublic static String collectionToDelimitedString(Collection<?> coll, String delim)
Collection into a delimited String (e.g. CSV).
Useful for toString() implementations.
coll - the Collection to convertdelim - the delimiter to use (typically a ",")Stringpublic static String collectionToCommaDelimitedString(Collection<?> coll)
Collection into a delimited String (e.g., CSV).
Useful for toString() implementations.
coll - the Collection to convertStringpublic static String deleteAny(String inString, String charsToDelete)
String.inString - the original StringcharsToDelete - a set of characters to delete.
E.g. "az\n" will delete 'a's, 'z's and new lines.Stringpublic static String getFilenameExtension(String path)
path - the file path (may be null)null if noneCopyright © 2016. All rights reserved.