public class StringUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
COMMA |
static String |
DOT |
static String |
EMPTY |
static String |
FOLDER_SEPARATOR |
static String |
LF |
static String |
TOP_PATH |
static String |
WINDOWS_FOLDER_SEPARATOR |
| 限定符和类型 | 方法和说明 |
|---|---|
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 |
capitalize(String str)
Capitalize a
String, changing the first letter to upper case as per Character.toUpperCase(char). |
static String |
cleanPath(String path)
Normalize the path by suppressing sequences like "path/.." and inner simple dots.
|
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 boolean |
contains(CharSequence str,
CharSequence searchStr)
Checks if CharSequence contains a search CharSequence.
|
static boolean |
containsIgnoreCase(CharSequence str,
CharSequence searchStr)
Checks if CharSequence contains a search CharSequence irrespective of case, handling
null. |
static String |
defaultEmptyIfBlank(String str)
Returns either the passed in CharSequence, or if the CharSequence is
empty or
null or whitespace only, the value of EmptyString. |
static String |
defaultIfBlank(String str,
String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
empty or
null or whitespace only, the value of defaultStr. |
static String |
defaultIfEmpty(String str,
String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
empty or
null, the value of defaultStr. |
static String |
deleteAny(String inString,
String charsToDelete)
Delete any character in a given
String. |
static String |
deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char). |
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 boolean |
equals(String str1,
String str2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters. |
static boolean |
equalsIgnoreCase(String str1,
String str2)
Compares two CharSequences, returning
true if they represent
equal sequences of characters, ignoring case. |
static String |
getFilename(String path)
Extract the filename from the given Java resource path, e.g.
|
static boolean |
hasLength(String str)
Check that the given
String is neither null nor of length 0. |
static boolean |
hasText(String str)
Check whether the given
String contains actual text. |
static boolean |
isAnyBlank(CharSequence... css)
Checks if any one of the CharSequences are blank ("") or null and not whitespace only..
|
static boolean |
isBlank(CharSequence cs)
Checks if a string is empty (""), null and whitespace only.
|
static boolean |
isEmpty(String str)
Checks if a str is empty ("") or null.
|
static boolean |
isNoneBlank(CharSequence... css)
Checks if none of the CharSequences are blank ("") or null and whitespace only..
|
static boolean |
isNotBlank(String str)
Checks if a string is not empty (""), not null and not whitespace only.
|
static boolean |
isNotEmpty(String str)
Checks if a str is not empty ("") or not null.
|
static String |
join(Collection collection,
String separator)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static String |
newStringForUtf8(byte[] bytes)
Create a string with encoding format as utf8.
|
static String |
replace(String inString,
String oldPattern,
String newPattern)
Replace all occurrences of a substring within a string with another string.
|
static String[] |
split(String str,
String separatorChars)
Splits the provided text into an array with a maximum length, separators specified.
|
static boolean |
startsWith(CharSequence str,
CharSequence prefix)
Check if a CharSequence starts with a specified prefix.
|
static boolean |
startsWithIgnoreCase(CharSequence str,
CharSequence prefix)
Case insensitive check if a CharSequence starts with a specified prefix.
|
static String |
substringBetween(String str,
String open,
String close)
Substring between two index.
|
static String[] |
tokenizeToStringArray(String str,
String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens)
|
static String[] |
toStringArray(Collection<String> collection)
Copy the given
Collection into a String array. |
static String |
trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling
null by returning null. |
public static String newStringForUtf8(byte[] bytes)
Create a string with encoding format as utf8.
bytes - the bytes that make up the stringpublic static boolean isBlank(CharSequence cs)
Checks if a string is empty (""), null and whitespace only.
cs - the string to checktrue if the string is empty and null and whitespacepublic static boolean isNotBlank(String str)
Checks if a string is not empty (""), not null and not whitespace only.
str - the string to check, may be nulltrue if the string is not empty and not null and not whitespacepublic static boolean isNotEmpty(String str)
Checks if a str is not empty ("") or not null.
str - the str to check, may be nulltrue if the str is not empty or not nullpublic static boolean isEmpty(String str)
Checks if a str is empty ("") or null.
str - the str to check, may be nulltrue if the str is empty or nullpublic static String defaultIfEmpty(String str, String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
empty or null, the value of defaultStr.
str - the CharSequence to check, may be nulldefaultStr - the default CharSequence to return if the input is empty ("") or null, may be nullpublic static String defaultIfBlank(String str, String defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
empty or null or whitespace only, the value of defaultStr.
str - the CharSequence to check, may be null, may be whitespace onlydefaultStr - the default CharSequence to return if the input is empty ("") or null, may be nullpublic static String defaultEmptyIfBlank(String str)
Returns either the passed in CharSequence, or if the CharSequence is
empty or null or whitespace only, the value of EmptyString.
str - the CharSequence to check, may be null, may be whitespace onlypublic static boolean equals(String str1, String str2)
Compares two CharSequences, returning true if they represent
equal sequences of characters.
str1 - the first string, may be nullstr2 - the second string, may be nulltrue if the string are equal (case-sensitive), or both nullObject.equals(Object)public static String trim(String str)
Removes control characters (char <= 32) from both
ends of this String, handling null by returning null.
str - the String to be trimmed, may be nullnull if null String inputpublic static String substringBetween(String str, String open, String close)
str - stringopen - start index to subclose - end index to subpublic static String join(Collection collection, String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
collection - the Collection of values to join together, may be nullseparator - the separator string to usenull if null array inputpublic static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr)
null.
Case-insensitivity is defined as by String.equalsIgnoreCase(String).
A null CharSequence will return false.
str - the CharSequence to check, may be nullsearchStr - the CharSequence to find, may be nullnull string inputpublic static boolean contains(CharSequence str, CharSequence searchStr)
str - the CharSequence to check, may be nullsearchStr - the CharSequence to find, may be nullpublic static boolean isNoneBlank(CharSequence... css)
Checks if none of the CharSequences are blank ("") or null and whitespace only..
css - the CharSequences to check, may be null or emptytrue if none of the CharSequences are blank or null or whitespace onlypublic static boolean isAnyBlank(CharSequence... css)
Checks if any one of the CharSequences are blank ("") or null and not whitespace only..
css - the CharSequences to check, may be null or emptytrue if any of the CharSequences are blank or null or whitespace onlypublic static boolean startsWith(CharSequence str, CharSequence prefix)
Check if a CharSequence starts with a specified prefix.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
str - the CharSequence to check, may be nullprefix - the prefix to find, may be nulltrue if the CharSequence starts with the prefix, case sensitive, or both nullString.startsWith(String)public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix)
Case insensitive check if a CharSequence starts with a specified prefix.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case insensitive.
str - the CharSequence to check, may be nullprefix - the prefix to find, may be nulltrue if the CharSequence starts with the prefix, case insensitive, or both nullString.startsWith(String)public static String deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char).
str - the String to delete whitespace from, may be nullnull if null String inputpublic static boolean equalsIgnoreCase(String str1, String str2)
Compares two CharSequences, returning true if they represent
equal sequences of characters, ignoring case.
str1 - the first string, may be nullstr2 - the second string, may be nulltrue if the string are equal, case insensitive, or both nullpublic static String[] split(String str, String separatorChars)
str - the String to parse, may be nullpublic static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
String into a String array via a StringTokenizer.
The given delimiters string can consist of any number of
delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single
character;
str - the String to tokenize (potentially null or empty)delimiters - the delimiter characters, assembled as a String (each of the characters is
individually considered as a delimiter)trimTokens - trim the tokens via String.trim()ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after
trimming; StringTokenizer will not consider subsequent delimiters as token in the first
place).StringTokenizer,
String.trim()public static String[] toStringArray(Collection<String> collection)
collection - the Collection to copy (potentially null or empty)String arraypublic static boolean hasText(String str)
String contains actual text.
More specifically, this method returns true if the
String is not null, its length is greater than 0, and it contains at least one non-whitespace
character.
str - the String to check (maybe null)true if the String is not null, its length is greater than 0, and it does not
contain whitespace onlyCharacter.isWhitespace(char)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.
NOTE that cleanPath should not be depended
upon in a security context. Other mechanisms should be used to prevent path-traversal issues.
path - the original pathpublic static String collectionToDelimitedString(Collection<?> coll, String delim)
Collection into a delimited String (e.g. CSV).
Useful for toString() implementations.
coll - the Collection to convert (potentially null or empty)delim - the delimiter to use (typically a ",")Stringpublic static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
coll - the Collection to convert (potentially null or empty)delim - the delimiter to use (typically a ",")prefix - the String to start each element withsuffix - the String to end each element withStringpublic static boolean hasLength(String str)
String is neither null nor of length 0.
Note: this method returns true for a String that
purely consists of whitespace.
str - the String to check (maybe null)true if the String is not null and has lengthhasText(String)public 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 a bunch of potential delimiter
characters, in contrast to tokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean).
str - the input String (potentially null or empty)delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual
delimiter characters)tokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean)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 a bunch of potential delimiter
characters, in contrast to tokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean).
str - the input String (potentially null or empty)delimiter - 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 StringtokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean)public 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 replace(String inString, String oldPattern, String newPattern)
inString - String to examineoldPattern - String to replacenewPattern - String to insertString with the replacementspublic 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 getFilename(String path)
"myPath/myFile.txt" → "myFile.txt".path - the file path (maybe null)null if nonepublic static String capitalize(String str)
String, changing the first letter to upper case as per Character.toUpperCase(char).
No other letters are changed.str - the String to capitalizeStringCopyright © 2018–2024 Alibaba Group. All rights reserved.