|
|||||||||
com.easyjf.util
类 StringUtils
java.lang.Object
com.easyjf.util.StringUtils
public abstract class StringUtils
extends java.lang.Object
Miscellaneous string utility methods. Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of string utilities.
This class delivers some simple functionality that should really be provided by the core Java String and StringBuffer classes, such as the ability to replace all occurrences of a given substring in a target string. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.
自从:
16 April 2001
作者:
Rod Johnson, Juergen Hoeller, Keith Donald, Rob Harrop
另请参见:
org.apache.commons.lang.StringUtils
方法摘要 |
|
static java.lang.String[] |
addStringToArray(java.lang.String[] array, java.lang.String str) |
static java.lang.String |
applyRelativePath(java.lang.String path, java.lang.String relativePath) |
static java.lang.String |
arrayToCommaDelimitedString(java.lang.Object[] arr) |
static java.lang.String |
arrayToDelimitedString(java.lang.Object[] arr, java.lang.String delim) |
static java.lang.String |
capitalize(java.lang.String str) |
static java.lang.String |
cleanPath(java.lang.String path) |
static java.lang.String |
collectionToCommaDelimitedString(java.util.Collection coll) |
static java.lang.String |
collectionToDelimitedString(java.util.Collection coll, java.lang.String delim) |
static java.lang.String |
collectionToDelimitedString(java.util.Collection coll, java.lang.String delim, java.lang.String prefix, java.lang.String suffix) |
static java.util.Set |
commaDelimitedListToSet(java.lang.String str) |
static java.lang.String[] |
commaDelimitedListToStringArray(java.lang.String str) |
static int |
countOccurrencesOf(java.lang.String str, java.lang.String sub) |
static java.lang.String |
delete(java.lang.String inString, java.lang.String pattern) |
static java.lang.String |
deleteAny(java.lang.String inString, java.lang.String charsToDelete) |
static java.lang.String[] |
delimitedListToStringArray(java.lang.String str, java.lang.String delimiter) |
static boolean |
endsWithIgnoreCase(java.lang.String str, java.lang.String suffix) |
static java.lang.String |
getFilename(java.lang.String path) |
static java.lang.String |
getFilenameExtension(java.lang.String path) |
static boolean |
hasLength(java.lang.String str) |
static boolean |
hasText(java.lang.String str) |
static java.util.Locale |
parseLocaleString(java.lang.String localeString) |
static boolean |
pathEquals(java.lang.String path1, java.lang.String path2) |
static java.lang.String |
quote(java.lang.String str) |
static java.lang.Object |
quoteIfString(java.lang.Object obj) |
static java.lang.String[] |
removeDuplicateStrings(java.lang.String[] array) |
static java.lang.String |
replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern) |
static java.lang.String[] |
sortStringArray(java.lang.String[] array) |
static java.lang.String[] |
split(java.lang.String toSplit, java.lang.String delimiter) |
static java.util.Properties |
splitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter) |
static java.util.Properties |
splitArrayElementsIntoProperties(java.lang.String[] array, java.lang.String delimiter, java.lang.String charsToDelete) |
static boolean |
startsWithIgnoreCase(java.lang.String str, java.lang.String prefix) |
static java.lang.String |
stripFilenameExtension(java.lang.String path) |
static java.lang.String[] |
tokenizeToStringArray(java.lang.String str, java.lang.String delimiters) |
static java.lang.String[] |
tokenizeToStringArray(java.lang.String str, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) |
static java.lang.String[] |
toStringArray(java.util.Collection collection) |
static java.lang.String |
trimLeadingWhitespace(java.lang.String str) |
static java.lang.String |
trimTrailingWhitespace(java.lang.String str) |
static java.lang.String |
trimWhitespace(java.lang.String str) |
static java.lang.String |
uncapitalize(java.lang.String str) |
static java.lang.String |
unqualify(java.lang.String qualifiedName) |
static java.lang.String |
unqualify(java.lang.String qualifiedName, char separator) |
从类 java.lang.Object 继承的方法 |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
public StringUtils()
public static boolean hasLength(java.lang.String str)
Check if a String has length.
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true
参数:
str - the String to check, may be null
返回:
true if the String is not null and has length
public static boolean hasText(java.lang.String str)
Check if a String has text. More specifically, returns true if the string not null, it's length is > 0, and it has at least one non-whitespace character.
StringUtils.hasText(null) = false
StringUtils.hasText("") = false
StringUtils.hasText(" ") = false
StringUtils.hasText("12345") = true
StringUtils.hasText(" 12345 ") = true
参数:
str - the String to check, may be null
返回:
true if the String is not null, length > 0, and not whitespace only
另请参见:
Character.isWhitespace(char)
public static java.lang.String trimWhitespace(java.lang.String str)
Trim leading and trailing whitespace from the given String.
参数:
str - the String to check
返回:
the trimmed String
另请参见:
Character.isWhitespace(char)
public static java.lang.String trimLeadingWhitespace(java.lang.String str)
Trim leading whitespace from the given String.
参数:
str - the String to check
返回:
the trimmed String
另请参见:
Character.isWhitespace(char)
public static java.lang.String trimTrailingWhitespace(java.lang.String str)
Trim trailing whitespace from the given String.
参数:
str - the String to check
返回:
the trimmed String
另请参见:
Character.isWhitespace(char)
public static boolean startsWithIgnoreCase(java.lang.String str,
java.lang.String prefix)
Test if the given String starts with the specified prefix, ignoring upper/lower case.
参数:
str - the String to check
prefix - the prefix to look for
另请参见:
String.startsWith(java.lang.String, int)
public static boolean endsWithIgnoreCase(java.lang.String str,
java.lang.String suffix)
Test if the given String ends with the specified suffix, ignoring upper/lower case.
参数:
str - the String to check
suffix - the suffix to look for
另请参见:
String.endsWith(java.lang.String)
public static int countOccurrencesOf(java.lang.String str,
java.lang.String sub)
Count the occurrences of the substring in string s.
参数:
str - string to search in. Return 0 if this is null.
sub - string to search for. Return 0 if this is null.
public static java.lang.String replace(java.lang.String inString,
java.lang.String oldPattern,
java.lang.String newPattern)
Replace all occurences of a substring within a string with another string.
参数:
inString - String to examine
oldPattern - String to replace
newPattern - String to insert
返回:
a String with the replacements
public static java.lang.String delete(java.lang.String inString,
java.lang.String pattern)
Delete all occurrences of the given substring.
参数:
pattern - the pattern to delete all occurrences of
public static java.lang.String deleteAny(java.lang.String inString,
java.lang.String charsToDelete)
Delete any character in a given string.
参数:
charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
public static java.lang.String quote(java.lang.String str)
Quote the given String with single quotes.
参数:
str - the input String (e.g. "myString")
返回:
the quoted String (e.g. "'myString'"), or null if the input was null
public static java.lang.Object quoteIfString(java.lang.Object obj)
Turn the given Object into a String with single quotes if it is a String; keeping the Object as-is else.
参数:
obj - the input Object (e.g. "myString")
返回:
the quoted String (e.g. "'myString'"), or the input object as-is if not a String
public static java.lang.String unqualify(java.lang.String qualifiedName)
Unqualify a string qualified by a '.' dot character. For example, "this.name.is.qualified", returns "qualified".
参数:
qualifiedName - the qualified name
public static java.lang.String unqualify(java.lang.String qualifiedName,
char separator)
Unqualify a string qualified by a separator character. For example, "this:name:is:qualified" returns "qualified" if using a ':' separator.
参数:
qualifiedName - the qualified name
separator - the separator
public static java.lang.String capitalize(java.lang.String str)
Capitalize a String, changing the first letter to upper case as per Character.toUpperCase(char). No other letters are changed.
参数:
str - the String to capitalize, may be null
返回:
the capitalized String, null if null
public static java.lang.String uncapitalize(java.lang.String str)
Uncapitalize a String, changing the first letter to lower case as per Character.toLowerCase(char). No other letters are changed.
参数:
str - the String to uncapitalize, may be null
返回:
the uncapitalized String, null if null
public static java.lang.String getFilename(java.lang.String path)
Extract the filename from the given path, e.g. "mypath/myfile.txt" -> "myfile.txt".
参数:
path - the file path (may be null)
返回:
the extracted filename, or null if none
public static java.lang.String getFilenameExtension(java.lang.String path)
Extract the filename extension from the given path, e.g. "mypath/myfile.txt" -> "txt".
参数:
path - the file path (may be null)
返回:
the extracted filename extension, or null if none
public static java.lang.String stripFilenameExtension(java.lang.String path)
Strip the filename extension from the given path, e.g. "mypath/myfile.txt" -> "mypath/myfile".
参数:
path - the file path (may be null)
返回:
the path with stripped filename extension, or null if none
public static java.lang.String applyRelativePath(java.lang.String path,
java.lang.String relativePath)
Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators);
参数:
path - the path to start from (usually a full file path)
relativePath - the relative path to apply (relative to the full file path above)
返回:
the full file path that results from applying the relative path
public static java.lang.String cleanPath(java.lang.String path)
Normalize the path by suppressing sequences like "path/.." and inner simple dots.
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
参数:
path - the original path
返回:
the normalized path
public static boolean pathEquals(java.lang.String path1,
java.lang.String path2)
Compare two paths after normalization of them.
参数:
path1 - First path for comparizon
path2 - Second path for comparizon
返回:
whether the two paths are equivalent after normalization
public static java.util.Locale parseLocaleString(java.lang.String localeString)
Parse the given locale string into a java.util.Locale. This is the inverse operation of Locale's toString.
参数:
localeString - the locale string, following java.util.Locale's toString format ("en", "en_UK", etc). Also accepts spaces as separators, as alternative to underscores.
返回:
a corresponding Locale instance
public static java.lang.String[] addStringToArray(java.lang.String[] array,
java.lang.String str)
Append the given String to the given String array, returning a new array consisting of the input array contents plus the given String.
参数:
array - the array to append to (can be null)
str - the String to append
返回:
the new array (never null)
public static java.lang.String[] sortStringArray(java.lang.String[] array)
Turn given source String array into sorted array.
参数:
array - the source array
返回:
the sorted array (never null)
public static java.lang.String[] toStringArray(java.util.Collection collection)
Copy the given Collection into a String array. The Collection must contain String elements only.
参数:
collection - the Collection to copy
返回:
the String array (null if the Collection was null as well)
public static java.lang.String[] removeDuplicateStrings(java.lang.String[] array)
Remove duplicate Strings from the given array. Also sorts the array, as it uses a TreeSet.
参数:
array - the String array
返回:
an array without duplicates, in natural sort order
public static java.lang.String[] split(java.lang.String toSplit,
java.lang.String delimiter)
Split a String at the first occurrence of the delimiter. Does not include the delimiter in the result.
参数:
toSplit - the string to split
delimiter - to split the string up with
返回:
a two element array with index 0 being before the delimiter, and index 1 being after the delimiter (neither element includes the delimiter); or null if the delimiter wasn't found in the given input String
splitArrayElementsIntoProperties
public static java.util.Properties splitArrayElementsIntoProperties(java.lang.String[] array,
java.lang.String delimiter)
Take an array Strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.
Will trim both the key and value before adding them to the Properties instance.
参数:
array - the array to process
delimiter - to split each element using (typically the equals symbol)
返回:
a Properties instance representing the array contents, or null if the array to process was null or empty
splitArrayElementsIntoProperties
public static java.util.Properties splitArrayElementsIntoProperties(java.lang.String[] array,
java.lang.String delimiter,
java.lang.String charsToDelete)
Take an array Strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.
Will trim both the key and value before adding them to the Properties instance.
参数:
array - the array to process
delimiter - to split each element using (typically the equals symbol)
charsToDelete - one or more characters to remove from each element prior to attempting the split operation (typically the quotation mark symbol), or null if no removal should occur
返回:
a Properties instance representing the array contents, or null if the array to process was null or empty
tokenizeToStringArray
public static java.lang.String[] tokenizeToStringArray(java.lang.String str,
java.lang.String delimiters)
Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.
The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray
参数:
str - the String to tokenize
delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).
返回:
an array of the tokens
另请参见:
StringTokenizer, String.trim(), delimitedListToStringArray(java.lang.String, java.lang.String)
public static java.lang.String[] tokenizeToStringArray(java.lang.String str,
java.lang.String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens)
Tokenize the given String into a String array via a StringTokenizer.
The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray
参数:
str - the String to tokenize
delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
trimTokens - trim the tokens via String's 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).
返回:
an array of the tokens
另请参见:
StringTokenizer, String.trim(), delimitedListToStringArray(java.lang.String, java.lang.String)
public static java.lang.String[] delimitedListToStringArray(java.lang.String str,
java.lang.String delimiter)
Take a String which is a delimited list and convert it to a String array.
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 String
delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
返回:
an array of the tokens in the list
另请参见:
tokenizeToStringArray(java.lang.String, java.lang.String)
commaDelimitedListToStringArray
public static java.lang.String[] commaDelimitedListToStringArray(java.lang.String str)
Convert a CSV list into an array of Strings.
参数:
str - CSV list
返回:
an array of Strings, or the empty array if s is null
public static java.util.Set commaDelimitedListToSet(java.lang.String str)
Convenience method to convert a CSV string list to a set. Note that this will suppress duplicates.
参数:
str - CSV String
返回:
a Set of String entries in the list
public static java.lang.String arrayToDelimitedString(java.lang.Object[] arr,
java.lang.String delim)
Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
参数:
arr - array to display. Elements may be of any type (toString will be called on each element).
delim - delimiter to use (probably a ",")
collectionToDelimitedString
public static java.lang.String collectionToDelimitedString(java.util.Collection coll,
java.lang.String delim,
java.lang.String prefix,
java.lang.String suffix)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
参数:
coll - Collection to display
delim - delimiter to use (probably a ",")
prefix - string to start each element with
suffix - string to end each element with
public static java.lang.String collectionToDelimitedString(java.util.Collection coll,
java.lang.String delim)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
参数:
coll - Collection to display
delim - delimiter to use (probably a ",")
public static java.lang.String arrayToCommaDelimitedString(java.lang.Object[] arr)
Convenience method to return a String array as a CSV String. E.g. useful for toString() implementations.
参数:
arr - array to display. Elements may be of any type (toString will be called on each element).
collectionToCommaDelimitedString
public static java.lang.String collectionToCommaDelimitedString(java.util.Collection coll)
Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.
参数:
coll - Collection to display
|
|||||||||