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 String
applyRelativePath
(String path, String relativePath) Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e.static String
arrayToDelimitedString
(Object[] arr, String delim) Convert aString
array into a delimitedString
(e.g.static String
capitalize
(String str) Capitalize aString
, changing the first letter to upper case as per {link Character#toUpperCase(char)}.static String
Normalize the path by suppressing sequences like "path/.." and inner simple dots.static String
collectionToDelimitedString
(Collection<?> coll, String delim) Convert aCollection
into a delimitedString
(e.g.static String
collectionToDelimitedString
(Collection<?> coll, String delim, String prefix, String suffix) Convert a {link Collection} to a delimitedString
(e.g.static String
Delete any character in a givenString
.static String[]
delimitedListToStringArray
(String str, String delimiter) Take aString
that is a delimited list and convert it into aString
array.static String[]
delimitedListToStringArray
(String str, String delimiter, String charsToDelete) Take aString
that is a delimited list and convert it into aString
array.static boolean
endsWithIgnoreCase
(String str, String suffix) Test if the givenString
ends with the specified suffix, ignoring upper/lower case.static String
getFilename
(String path) Extract the filename from the given Java resource path, e.g.static boolean
Check that the givenString
is neithernull
nor of length 0.static String
Replace all occurrences of a substring within a string with another string.static boolean
startsWithIgnoreCase
(String str, String prefix) Test if the givenString
starts with the specified prefix, ignoring upper/lower case.static String[]
tokenizeToStringArray
(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the givenString
into aString
array via a {link StringTokenizer}.static String[]
toStringArray
(Collection<String> collection) Copy the given {link Collection} into aString
array.
-
Method Details
-
hasLength
Check that the givenString
is neithernull
nor of length 0.Note: this method returns
true
for aString
that purely consists of whitespace.- Parameters:
str
- theString
to check (may benull
)- Returns:
true
if theString
is notnull
and has length see #hasLength(CharSequence) see #hasText(String)
-
startsWithIgnoreCase
Test if the givenString
starts with the specified prefix, ignoring upper/lower case.- Parameters:
str
- theString
to checkprefix
- the prefix to look for see java.lang.String#startsWith
-
endsWithIgnoreCase
Test if the givenString
ends with the specified suffix, ignoring upper/lower case.- Parameters:
str
- theString
to 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
-String
to examineoldPattern
-String
to replacenewPattern
-String
to insert- Returns:
- a
String
with the replacements
-
deleteAny
Delete any character in a givenString
.- Parameters:
inString
- the originalString
charsToDelete
- 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
- theString
to 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
null
if 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
cleanPath
should 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 aString
array.The
Collection
must containString
elements only.- Parameters:
collection
- theCollection
to copy (potentiallynull
or empty)- Returns:
- the resulting
String
array
-
tokenizeToStringArray
public static String[] tokenizeToStringArray(@Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the givenString
into aString
array via a {link 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; for multi-character delimiters, consider using {link #delimitedListToStringArray}.- Parameters:
str
- theString
to tokenize (potentiallynull
or 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 aString
that is a delimited list and convert it into aString
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 {link #tokenizeToStringArray}.- Parameters:
str
- the inputString
(potentiallynull
or 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 aString
that is a delimited list and convert it into aString
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 {link #tokenizeToStringArray}.- Parameters:
str
- the inputString
(potentiallynull
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 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
- theCollection
to convert (potentiallynull
or empty)delim
- the delimiter to use (typically a ",")prefix
- theString
to start each element withsuffix
- theString
to end each element with- Returns:
- the delimited
String
-
collectionToDelimitedString
Convert aCollection
into a delimitedString
(e.g. CSV).Useful for
toString()
implementations.- Parameters:
coll
- theCollection
to convert (potentiallynull
or empty)delim
- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
arrayToDelimitedString
Convert aString
array into a delimitedString
(e.g. CSV).Useful for
toString()
implementations.- Parameters:
arr
- the array to display (potentiallynull
or empty)delim
- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-