Class StringUtils
Mainly for internal use within the framework; consider
Apache'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 {link String} and {link StringBuilder} classes. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.
- Since:
- 16 April 2001
- Author:
- Rod Johnson, Juergen Hoeller, Keith Donald, Rob Harrop, Rick Evans, Arjen Poutsma, Sam Brannen, Brian Clozel
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringapplyRelativePath(String path, String relativePath) Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e. "/" separators).static StringarrayToDelimitedString(Object[] arr, String delim) Convert aStringarray into a delimitedString(e.g.static Stringcapitalize(String str) Capitalize aString, changing the first letter to upper case as per {link Character#toUpperCase(char)}.static StringNormalize the path by suppressing sequences like "path/.." and inner simple dots.static StringcollectionToDelimitedString(Collection<?> coll, String delim) Convert aCollectioninto a delimitedString(e.g.static StringcollectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) Convert a {link Collection} to a delimitedString(e.g.static StringDelete any character in a givenString.static String[]delimitedListToStringArray(String str, String delimiter) Take aStringthat is a delimited list and convert it into aStringarray.static String[]delimitedListToStringArray(String str, String delimiter, String charsToDelete) Take aStringthat is a delimited list and convert it into aStringarray.static booleanendsWithIgnoreCase(String str, String suffix) Test if the givenStringends with the specified suffix, ignoring upper/lower case.static StringgetFilename(String path) Extract the filename from the given Java resource path, e.g.static booleanCheck that the givenStringis neithernullnor of length 0.static StringReplace all occurrences of a substring within a string with another string.static booleanstartsWithIgnoreCase(String str, String prefix) Test if the givenStringstarts with the specified prefix, ignoring upper/lower case.static String[]tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the givenStringinto aStringarray via a {link StringTokenizer}.static String[]toStringArray(Collection<String> collection) Copy the given {link Collection} into aStringarray.
-
Method Details
-
hasLength
Check that the givenStringis neithernullnor of length 0.Note: this method returns
truefor aStringthat purely consists of whitespace.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis notnulland has length see #hasLength(CharSequence) see #hasText(String)
-
startsWithIgnoreCase
Test if the givenStringstarts with the specified prefix, ignoring upper/lower case.- Parameters:
str- theStringto checkprefix- the prefix to look for see java.lang.String#startsWith
-
endsWithIgnoreCase
Test if the givenStringends with the specified suffix, ignoring upper/lower case.- Parameters:
str- theStringto checksuffix- the suffix to look for see java.lang.String#endsWith
-
replace
Replace all occurrences of a substring within a string with another string.- Parameters:
inString-Stringto examineoldPattern-Stringto replacenewPattern-Stringto insert- Returns:
- a
Stringwith the replacements
-
deleteAny
Delete any character in a givenString.- Parameters:
inString- the originalStringcharsToDelete- a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.- Returns:
- the resulting
String
-
capitalize
Capitalize aString, changing the first letter to upper case as per {link Character#toUpperCase(char)}. No other letters are changed.- Parameters:
str- theStringto capitalize- Returns:
- the capitalized
String
-
getFilename
Extract the filename from the given Java resource path, e.g."mypath/myfile.txt" → "myfile.txt".- Parameters:
path- the file path (may benull)- Returns:
- the extracted filename, or
nullif none
-
applyRelativePath
Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e. "/" separators).- Parameters:
path- the path to start from (usually a full file path)relativePath- the relative path to apply (relative to the full file path above)- Returns:
- the full file path that results from applying the relative path
-
cleanPath
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.
NOTE that
cleanPathshould not be depended upon in a security context. Other mechanisms should be used to prevent path-traversal issues.- Parameters:
path- the original path- Returns:
- the normalized path
-
toStringArray
Copy the given {link Collection} into aStringarray.The
Collectionmust containStringelements only.- Parameters:
collection- theCollectionto copy (potentiallynullor empty)- Returns:
- the resulting
Stringarray
-
tokenizeToStringArray
public static String[] tokenizeToStringArray(@Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the givenStringinto aStringarray via a {link StringTokenizer}.The given
delimitersstring 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; for multi-character delimiters, consider using {link #delimitedListToStringArray}.- Parameters:
str- theStringto tokenize (potentiallynullor empty)delimiters- the delimiter characters, assembled as aString(each of the characters is individually considered as a delimiter)trimTokens- trim the tokens via {link 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).- Returns:
- an array of the tokens see java.util.StringTokenizer see String#trim() see #delimitedListToStringArray
-
delimitedListToStringArray
Take aStringthat is a delimited list and convert it into aStringarray.A single
delimitermay 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 {link #tokenizeToStringArray}.- Parameters:
str- the inputString(potentiallynullor empty)delimiter- the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)- Returns:
- an array of the tokens in the list see #tokenizeToStringArray
-
delimitedListToStringArray
public static String[] delimitedListToStringArray(@Nullable String str, @Nullable String delimiter, @Nullable String charsToDelete) Take aStringthat is a delimited list and convert it into aStringarray.A single
delimitermay 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 {link #tokenizeToStringArray}.- Parameters:
str- the inputString(potentiallynullor 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 aString- Returns:
- an array of the tokens in the list see #tokenizeToStringArray
-
collectionToDelimitedString
public static String collectionToDelimitedString(@Nullable Collection<?> coll, String delim, String prefix, String suffix) Convert a {link Collection} to a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
coll- theCollectionto convert (potentiallynullor empty)delim- the delimiter to use (typically a ",")prefix- theStringto start each element withsuffix- theStringto end each element with- Returns:
- the delimited
String
-
collectionToDelimitedString
Convert aCollectioninto a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
coll- theCollectionto convert (potentiallynullor empty)delim- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
arrayToDelimitedString
Convert aStringarray into a delimitedString(e.g. CSV).Useful for
toString()implementations.- Parameters:
arr- the array to display (potentiallynullor empty)delim- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-