Class StringUtils
- java.lang.Object
-
- spark.utils.StringUtils
-
public abstract class StringUtils extends java.lang.Object
MiscellaneousString
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
andStringBuilder
classes, such as the ability toreplace(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 Modifier and Type Field Description private static java.lang.String
CURRENT_PATH
private static java.lang.String
FOLDER_SEPARATOR
private static java.lang.String
TOP_PATH
private static java.lang.String
WINDOWS_FOLDER_SEPARATOR
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.static java.lang.String
cleanPath(java.lang.String path)
Normalize the path by suppressing sequences like "path/.." and inner simple dots.static java.lang.String
collectionToDelimitedString(java.util.Collection<?> coll, java.lang.String delim)
Convenience method to return a Collection as a delimited (e.g.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.static java.lang.String
deleteAny(java.lang.String inString, java.lang.String charsToDelete)
Delete any character in a given String.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.static java.lang.String[]
delimitedListToStringArray(java.lang.String str, java.lang.String delimiter, java.lang.String charsToDelete)
Take a String which is a delimited list and convert it to a String array.static java.lang.String
getFilename(java.lang.String path)
Extract the filename from the given path.static boolean
hasLength(java.lang.CharSequence str)
Check that the given CharSequence is neithernull
nor of length 0.static boolean
hasLength(java.lang.String str)
Check that the given String is neithernull
nor of length 0.static boolean
isBlank(java.lang.CharSequence cs)
static boolean
isEmpty(java.lang.Object str)
Check whether the given String is empty.static boolean
isNotBlank(java.lang.CharSequence cs)
static boolean
isNotEmpty(java.lang.String str)
Checks if the given String is not emptystatic java.lang.String
removeLeadingAndTrailingSlashesFrom(java.lang.String string)
static java.lang.String
replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
Replace all occurrences of a substring within a string with another string.static java.lang.String
toString(byte[] bytes, java.lang.String encoding)
static java.lang.String[]
toStringArray(java.util.Collection<java.lang.String> collection)
Copy the given Collection into a String array.
-
-
-
Field Detail
-
FOLDER_SEPARATOR
private static final java.lang.String FOLDER_SEPARATOR
- See Also:
- Constant Field Values
-
WINDOWS_FOLDER_SEPARATOR
private static final java.lang.String WINDOWS_FOLDER_SEPARATOR
- See Also:
- Constant Field Values
-
TOP_PATH
private static final java.lang.String TOP_PATH
- See Also:
- Constant Field Values
-
CURRENT_PATH
private static final java.lang.String CURRENT_PATH
- See Also:
- Constant Field Values
-
-
Method Detail
-
isBlank
public static boolean isBlank(java.lang.CharSequence cs)
-
isNotBlank
public static boolean isNotBlank(java.lang.CharSequence cs)
-
isEmpty
public static boolean isEmpty(java.lang.Object str)
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
public static boolean isNotEmpty(java.lang.String str)
Checks if the given String is not empty- Parameters:
str
- the candidate String- Returns:
- if the String is not empty
-
hasLength
public static boolean hasLength(java.lang.CharSequence str)
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
public static boolean hasLength(java.lang.String str)
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:
hasLength(CharSequence)
-
replace
public static java.lang.String replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
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
public static java.lang.String deleteAny(java.lang.String inString, java.lang.String charsToDelete)
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
public static java.lang.String getFilename(java.lang.String path)
Extract the filename from the given path.- Parameters:
path
- the file path (may benull
)- Returns:
- the extracted filename, or
null
if none
-
applyRelativePath
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).- 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
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.
- Parameters:
path
- the original path- Returns:
- the normalized path
-
toStringArray
public static java.lang.String[] toStringArray(java.util.Collection<java.lang.String> collection)
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
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
.- 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 java.lang.String[] delimitedListToStringArray(java.lang.String str, java.lang.String delimiter, java.lang.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 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 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
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 fortoString()
implementations.- Parameters:
coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")- Returns:
- the delimited String
-
toString
public static java.lang.String toString(byte[] bytes, java.lang.String encoding)
-
removeLeadingAndTrailingSlashesFrom
public static java.lang.String removeLeadingAndTrailingSlashesFrom(java.lang.String string)
-
-