public abstract class StringUtils extends Object
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.
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
applyRelativePath(String path,
String relativePath)
Apply the given relative path to the given path,
assuming standard Java folder separation (i.e.
|
static String |
cleanPath(String path)
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 |
deleteAny(String inString,
String charsToDelete)
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 neither
null nor of length 0. |
static boolean |
hasLength(String str)
Check that the given String is neither
null nor of length 0. |
static boolean |
isBlank(CharSequence cs) |
static boolean |
isEmpty(Object str)
Check whether the given String is empty.
|
static boolean |
isNotBlank(CharSequence cs) |
static boolean |
isNotEmpty(String str)
Checks if the given String is not empty
|
static String |
removeLeadingAndTrailingSlashesFrom(String string) |
static String |
replace(String inString,
String oldPattern,
String newPattern)
Replace all occurrences of a substring within a string with
another string.
|
static String |
toString(byte[] bytes,
String encoding) |
static String[] |
toStringArray(Collection<String> collection)
Copy the given Collection into a String array.
|
public static boolean isBlank(CharSequence cs)
public static boolean isNotBlank(CharSequence cs)
public static boolean isEmpty(Object str)
This method accepts any Object as an argument, comparing it to
null
and the empty String. As a consequence, this method
will never return true
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.
str
- the candidate Stringpublic static boolean isNotEmpty(String str)
str
- the candidate Stringpublic static boolean hasLength(CharSequence str)
null
nor of length 0.str
- the CharSequence to check (may be null
)true
if the CharSequence is not null and has lengthpublic static boolean hasLength(String str)
null
nor of length 0.
Note: Will return true
for a String that purely consists of whitespace.str
- the String to check (may be null
)true
if the String is not null and has lengthhasLength(CharSequence)
public static String replace(String inString, String oldPattern, String newPattern)
inString
- String to examineoldPattern
- String to replacenewPattern
- String to insertpublic static String deleteAny(String inString, String charsToDelete)
inString
- the original StringcharsToDelete
- a set of characters to delete.
E.g. "az\n" will delete 'a's, 'z's and new lines.public static String getFilename(String path)
path
- the file path (may be null
)null
if nonepublic static String applyRelativePath(String path, String relativePath)
path
- the path to start from (usually a full file path)relativePath
- the relative path to apply
(relative to the full file path above)public static String cleanPath(String path)
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
path
- the original pathpublic static String[] toStringArray(Collection<String> collection)
collection
- the Collection to copynull
if the passed-in
Collection was null
)public static String[] delimitedListToStringArray(String str, String delimiter)
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 Stringdelimiter
- the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
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 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.public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
toString()
implementations.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 withpublic static String collectionToDelimitedString(Collection<?> coll, String delim)
toString()
implementations.coll
- the Collection to displaydelim
- the delimiter to use (probably a ",")Copyright © 2017. All rights reserved.