Package com.aspectran.utils
Class StringUtils
java.lang.Object
com.aspectran.utils.StringUtils
Static utility methods pertaining to
String
or CharSequence
instances.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsWhitespace
(CharSequence chars) Check whether the givenCharSequence
contains any whitespace characters.static boolean
containsWhitespace
(String str) Check whether the givenString
contains any whitespace characters.static String
convertToHumanFriendlyByteSize
(long bytes) Convert byte size into human friendly format.static long
Convert byte size into machine friendly format.static String
emptyToNull
(String str) Returns the given string if it is nonempty;null
otherwise.static boolean
Test if the givenString
ends with the specified prefix character.static boolean
endsWithIgnoreCase
(String str, String suffix) Test if the givenString
ends with the specified suffix, ignoring upper/lower case.static boolean
hasLength
(CharSequence chars) Check that the givenCharSequence
is neithernull
nor of length 0.static boolean
Check that the givenString
is neithernull
nor of length 0.static boolean
hasText
(CharSequence chars) Check whether the givenCharSequence
contains actual text.static boolean
Check whether the givenString
contains actual text.static boolean
Returnstrue
if the given string is null or is the empty string.static String
joinCommaDelimitedList
(String[] arr) Convert aString
array into a comma delimitedString
(i.e., CSV).static String
joinCommaDelimitedList
(Collection<?> collection) Convert aCollection
into a comma delimitedString
(i.e., CSV).static String
nullToEmpty
(String str) Returns the given string if it is non-null; the empty string otherwise.static String
repeat
(char ch, int repeat) Returns padding using the specified delimiter repeated to a given length.static String
Replace all occurrences of a substring within a string with another string.static String
Replace all occurrences of a substring within a string with another string.static String
replaceLast
(String str, String searchStr, String replacement) Replace last occurrence of a string.static int
search
(CharSequence chars, char c) Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.static int
Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.static int
searchIgnoreCase
(CharSequence chars, char searchChar) Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.static int
searchIgnoreCase
(String str, String searchStr) Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.static String[]
Returns an array of strings separated by the delimiter string.static String[]
Returns an array of strings separated by the delimiter string.static String[]
Returns an array of strings separated by the delimiter string.static String[]
Returns an array of strings separated by the delimiter string.static String[]
Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.static boolean
startsWith
(String str, char prefix) Test if the givenString
starts with the specified prefix character.static boolean
startsWithIgnoreCase
(String str, String prefix) Test if the givenString
starts with the specified prefix, ignoring upper/lower case.static String
toDelimitedString
(Object[] arr, String delim) Convert aString
array into a delimitedString
(e.g.static String
toDelimitedString
(Collection<?> collection, String delim) Convert aCollection
into a delimitedString
(e.g.static String[]
Tokenize the givenString
into a String array via a StringTokenizer.static String[]
Tokenize the givenString
into aString
array via aStringTokenizer
.static String
toLineDelimitedString
(Object[] arr) Convert aString
array into a delimitedString
by a system-dependent line separator.static String
toLineDelimitedString
(Collection<?> collection) Convert aCollection
into a delimitedString
by a system-dependent line separator.static String[]
toStringArray
(Collection<String> collection) Copy the givenCollection
into aString
array.static String[]
toStringArray
(Enumeration<String> enumeration) Copy the givenEnumeration
into aString
array.static String
trimAllWhitespace
(String str) Trim all whitespace from the givenString
: leading, trailing, and in between characters.static String
trimLeadingCharacter
(String str, char leadingChar) Trim all occurrences of the supplied leading character from the givenString
.static String
Trim leading whitespace from the givenString
.static String
trimTrailingCharacter
(String str, char trailingChar) Trim all occurrences of the supplied trailing character from the givenString
.static String
Trim trailing whitespace from the givenString
.static String
trimWhitespace
(String str) Trim leading and trailing whitespace from the givenString
.
-
Field Details
-
EMPTY
Constant for an emptyString
.- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
isEmpty
Returnstrue
if the given string is null or is the empty string.- Parameters:
str
- a string reference to check- Returns:
true
if the string is null or is the empty string
-
nullToEmpty
Returns the given string if it is non-null; the empty string otherwise.- Parameters:
str
- the string to test and possibly return- Returns:
string
itself if it is non-null;""
if it is null
-
emptyToNull
Returns the given string if it is nonempty;null
otherwise.- Parameters:
str
- the string to test and possibly return- Returns:
string
itself if it is nonempty;null
if it is empty or null
-
hasLength
Check that the givenCharSequence
is neithernull
nor of length 0.Note: this method returns
true
for aCharSequence
that purely consists of whitespace.StringUtils.hasLength(null) = false StringUtils.hasLength("") = false StringUtils.hasLength(" ") = true StringUtils.hasLength("Hello") = true
- Parameters:
chars
- theCharSequence
to check (may benull
)- Returns:
true
if theCharSequence
is notnull
and has length- See Also:
-
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 Also:
-
hasText
Check whether the givenCharSequence
contains actual text.More specifically, this method returns
true
if theCharSequence
is notnull
, its length is greater than 0, and it contains at least one non-whitespace character.StringUtils.hasText(null) = false StringUtils.hasText("") = false StringUtils.hasText(" ") = false StringUtils.hasText("12345") = true StringUtils.hasText(" 12345 ") = true
- Parameters:
chars
- theCharSequence
to check (may benull
)- Returns:
true
if theCharSequence
is notnull
, its length is greater than 0, and it does not contain whitespace only- See Also:
-
hasText
Check whether the givenString
contains actual text.More specifically, this method returns
true
if theString
is notnull
, its length is greater than 0, and it contains at least one non-whitespace character.- Parameters:
str
- theString
to check (may benull
)- Returns:
true
if theString
is notnull
, its length is greater than 0, and it does not contain whitespace only- See Also:
-
containsWhitespace
Check whether the givenCharSequence
contains any whitespace characters.- Parameters:
chars
- theCharSequence
to check (maybenull
)- Returns:
true
if theCharSequence
is not empty and contains at least 1 whitespace character- See Also:
-
containsWhitespace
Check whether the givenString
contains any whitespace characters.- Parameters:
str
- theString
to check (may benull
)- Returns:
true
if theString
is not empty and contains at least 1 whitespace character- See Also:
-
trimWhitespace
Trim leading and trailing whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
-
trimAllWhitespace
Trim all whitespace from the givenString
: leading, trailing, and in between characters.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
-
trimLeadingWhitespace
Trim leading whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
-
trimTrailingWhitespace
Trim trailing whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
-
trimLeadingCharacter
Trim all occurrences of the supplied leading character from the givenString
.- Parameters:
str
- theString
to checkleadingChar
- the leading character to be trimmed- Returns:
- the trimmed
String
-
trimTrailingCharacter
Trim all occurrences of the supplied trailing character from the givenString
.- Parameters:
str
- theString
to checktrailingChar
- the trailing character to be trimmed- Returns:
- the trimmed
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- Returns:
true
if theString
starts with the prefix, case-insensitive, or bothnull
- See Also:
-
endsWithIgnoreCase
Test if the givenString
ends with the specified suffix, ignoring upper/lower case.- Parameters:
str
- theString
to checksuffix
- the suffix to look for- Returns:
true
if theString
ends with the suffix, case-insensitive, or bothnull
- See Also:
-
startsWith
Test if the givenString
starts with the specified prefix character.- Parameters:
str
- theString
to checkprefix
- the prefix character to look for- Returns:
- true if the string starts with the specified prefix; otherwise false
- See Also:
-
endsWith
Test if the givenString
ends with the specified prefix character.- Parameters:
str
- theString
to checksuffix
- the prefix character to look for- Returns:
- true if the string ends with the specified suffix; otherwise false
- See Also:
-
replace
Replace all occurrences of a substring within a string with another string.- Parameters:
str
-String
to examinesearch
-String
to replacereplacement
-String
to insert- Returns:
- a
String
with the replacements
-
replace
Replace all occurrences of a substring within a string with another string.- Parameters:
str
-String
to examinesearchList
-String
array to replacereplacementList
-String
array to insert- Returns:
- a
String
with the replacements
-
replaceLast
@NonNull public static String replaceLast(@NonNull String str, @NonNull String searchStr, @NonNull String replacement) Replace last occurrence of a string.- Parameters:
str
-String
to examinesearchStr
-String
to replacereplacement
-String
to insert- Returns:
- a
String
with the replacements
-
split
Returns an array of strings separated by the delimiter string.- Parameters:
str
- the string to be separateddelim
- the delimiter- Returns:
- an array, containing the splitted strings
-
split
Returns an array of strings separated by the delimiter string.- Parameters:
str
- the string to be separateddelim
- the delimitersize
- the size of the array- Returns:
- an array, containing the splitted strings
-
split
Returns an array of strings separated by the delimiter string.- Parameters:
str
- the string to be separateddelim
- the delimiter- Returns:
- an array, containing the splitted strings
-
split
Returns an array of strings separated by the delimiter string.- Parameters:
str
- the string to be separateddelim
- the delimitersize
- the size of the array- Returns:
- an array, containing the splitted strings
-
search
Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.- Parameters:
str
- the target stringsearchStr
- the string to find- Returns:
- the number of times the specified string was found
-
searchIgnoreCase
Returns the number of times the specified string was found in the target string, or 0 if there is no specified string. When searching for the specified string, it is not case-sensitive.- Parameters:
str
- the target stringsearchStr
- the string to find- Returns:
- the number of times the specified string was found
-
search
Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.- Parameters:
chars
- the target stringc
- the character to find- Returns:
- the number of times the specified character was found
-
searchIgnoreCase
Returns the number of times the specified character was found in the target string, or 0 if there is no specified character. When searching for the specified character, it is not case-sensitive.- Parameters:
chars
- the target stringsearchChar
- the character to find- Returns:
- the number of times the specified character was found
-
tokenize
Tokenize the givenString
into a String array via a StringTokenizer.- Parameters:
str
- theString
to tokenizedelimiters
- the delimiter characters- Returns:
- an array of the tokens
-
tokenize
Tokenize the givenString
into aString
array via aStringTokenizer
.- Parameters:
str
- the String to tokenizedelimiters
- the delimiter characterstrim
- trim the tokens via String's trim- Returns:
- an array of the tokens
-
toDelimitedString
Convert aString
array into a delimitedString
(e.g. CSV).Useful for
toString()
implementations.- Parameters:
arr
- the array to displaydelim
- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
toDelimitedString
Convert aCollection
into a delimitedString
(e.g. CSV).Useful for
toString()
implementations.- Parameters:
collection
- the collectiondelim
- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
toLineDelimitedString
Convert aString
array into a delimitedString
by a system-dependent line separator.- Parameters:
arr
- the array to display- Returns:
- the delimited
String
-
toLineDelimitedString
Convert aCollection
into a delimitedString
by a system-dependent line separator.- Parameters:
collection
- the collection- Returns:
- the delimited
String
-
splitCommaDelimitedString
Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.- Parameters:
str
- the inputString
- Returns:
- an array of strings, or the empty array in case of empty input
-
joinCommaDelimitedList
Convert aString
array into a comma delimitedString
(i.e., CSV).- Parameters:
arr
- the array to display- Returns:
- the delimited
String
-
joinCommaDelimitedList
Convert aCollection
into a comma delimitedString
(i.e., CSV).- Parameters:
collection
- the collection- Returns:
- the delimited
String
-
toStringArray
- Parameters:
collection
- theCollection
to copy (potentiallynull
or empty)- Returns:
- the resulting
String
array
-
toStringArray
- Parameters:
enumeration
- theEnumeration
to copy (potentiallynull
or empty)- Returns:
- the resulting
String
array
-
repeat
Returns padding using the specified delimiter repeated to a given length.- Parameters:
ch
- character to repeatrepeat
- number of times to repeat char, negative treated as zero- Returns:
- String with repeated character
-
convertToHumanFriendlyByteSize
Convert byte size into human friendly format.- Parameters:
bytes
- the number of bytes- Returns:
- a human friendly byte size (includes units)
-
convertToMachineFriendlyByteSize
Convert byte size into machine friendly format.- Parameters:
bytes
- the human friendly byte size (includes units)- Returns:
- a number of bytes
- Throws:
NumberFormatException
- if failed parse given size
-