Class StringUtils
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 StringBuilder
classes, such as the ability to replace(java.lang.String, java.lang.String, java.lang.String)
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.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
applyRelativePath
(String path, String relativePath) Apply the given relative path to the given path, assuming standard Java folder separation (i.e.static String
Normalize the path by suppressing sequences like "path/.." and inner simple dots.static String
collectionToDelimitedString
(Collection<?> coll, String delim) Convenience method to return a Collection as a delimited (e.g.static String
collectionToDelimitedString
(Collection<?> coll, String delim, String prefix, String suffix) Convenience method to return a Collection as a delimited (e.g.static String
Delete any character in a given String.static String[]
delimitedListToStringArray
(String str, String delimiter) Take a String which is a delimited list and convert it to a String array.static String[]
delimitedListToStringArray
(String str, String delimiter, String charsToDelete) Take a String which is a delimited list and convert it to a String array.static String
getFilename
(String path) Extract the filename from the given path.static boolean
hasLength
(CharSequence str) Check that the given CharSequence is neithernull
nor of length 0.static boolean
Check that the given String is neithernull
nor of length 0.static boolean
isBlank
(CharSequence cs) static boolean
Check whether the given String is empty.static boolean
static boolean
isNotEmpty
(String str) Checks if the given String is not emptystatic String
static String
Replace all occurrences of a substring within a string with another string.static String
static String[]
toStringArray
(Collection<String> collection) Copy the given Collection into a String array.
-
Field Details
-
FOLDER_SEPARATOR
- See Also:
-
WINDOWS_FOLDER_SEPARATOR
- See Also:
-
TOP_PATH
- See Also:
-
CURRENT_PATH
- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
isBlank
-
isNotBlank
-
isEmpty
Check whether the given String is empty.This method accepts any Object as an argument, comparing it to
null
and the empty String. As a consequence, this method will never returntrue
for a non-null non-String object.The Object signature is useful for general attribute handling code that commonly deals with Strings but generally has to iterate over Objects since attributes may e.g. be primitive value objects as well.
- Parameters:
str
- the candidate String- Returns:
- if the String is empty
- Since:
- 3.2.1
-
isNotEmpty
Checks if the given String is not empty- Parameters:
str
- the candidate String- Returns:
- if the String is not empty
-
hasLength
Check that the given CharSequence is neithernull
nor of length 0.- Parameters:
str
- the CharSequence to check (may benull
)- Returns:
true
if the CharSequence is not null and has length
-
hasLength
Check that the given String is neithernull
nor of length 0. Note: Will returntrue
for a String that purely consists of whitespace.- Parameters:
str
- the String to check (may benull
)- Returns:
true
if the String is not null and has length- See Also:
-
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 given String.- Parameters:
inString
- the original StringcharsToDelete
- a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.- Returns:
- the resulting String
-
getFilename
Extract the filename from the given path.- Parameters:
path
- the file path (may benull
)- Returns:
- the extracted filename, or
null
if none
-
applyRelativePath
Apply the given relative path to the given 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.
- Parameters:
path
- the original path- Returns:
- the normalized path
-
toStringArray
Copy the given Collection into a String array. The Collection must contain String elements only.- Parameters:
collection
- the Collection to copy- Returns:
- the String array (
null
if the passed-in Collection wasnull
)
-
delimitedListToStringArray
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
.- Parameters:
str
- the input Stringdelimiter
- 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
-
delimitedListToStringArray
public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete) 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
.- Parameters:
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 String.- Returns:
- an array of the tokens in the list
-
collectionToDelimitedString
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful fortoString()
implementations.- Parameters:
coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")prefix
- the String to start each element withsuffix
- the String to end each element with- Returns:
- the delimited String
-
collectionToDelimitedString
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful fortoString()
implementations.- Parameters:
coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")- Returns:
- the delimited String
-
toString
-
removeLeadingAndTrailingSlashesFrom
-