Package com.aspectran.core.util
Class StringUtils
- java.lang.Object
-
- com.aspectran.core.util.StringUtils
-
public class StringUtils extends java.lang.Object
Static utility methods pertaining toString
orCharSequence
instances.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
EMPTY
The emptyString
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
containsWhitespace(java.lang.CharSequence str)
Check whether the givenCharSequence
contains any whitespace characters.static boolean
containsWhitespace(java.lang.String str)
Check whether the givenString
contains any whitespace characters.static java.lang.String
convertToHumanFriendlyByteSize(long size)
Convert byte size into human friendly format.static long
convertToMachineFriendlyByteSize(java.lang.String size)
Convert byte size into machine friendly format.static java.lang.String
emptyToNull(java.lang.String str)
Returns the given string if it is nonempty;null
otherwise.static boolean
endsWith(java.lang.String str, char suffix)
Test if the givenString
ends with the specified prefix character.static boolean
endsWithIgnoreCase(java.lang.String str, java.lang.String suffix)
Test if the givenString
ends with the specified suffix, ignoring upper/lower case.static boolean
hasLength(java.lang.CharSequence chars)
Check that the givenCharSequence
is neithernull
nor of length 0.static boolean
hasLength(java.lang.String str)
Check that the givenString
is neithernull
nor of length 0.static boolean
hasText(java.lang.CharSequence str)
Check whether the givenCharSequence
contains actual text.static boolean
hasText(java.lang.String str)
Check whether the givenString
contains actual text.static boolean
isEmpty(java.lang.String str)
Returnstrue
if the given string is null or is the empty string.static java.lang.String
joinCommaDelimitedList(java.lang.String[] arr)
Convert aString
array into a comma delimitedString
(i.e., CSV).static java.lang.String
joinCommaDelimitedList(java.util.Collection<?> list)
Convert aCollection
into a comma delimitedString
(i.e., CSV).static java.lang.String
nullToEmpty(java.lang.String str)
Returns the given string if it is non-null; the empty string otherwise.static java.util.Locale
parseLocaleString(java.lang.String localeString)
Parse the givenlocaleString
value into aLocale
.static java.util.TimeZone
parseTimeZoneString(java.lang.String timeZoneString)
Parse the giventimeZoneString
value into aTimeZone
.static java.lang.String
repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.static java.lang.String
replace(java.lang.String str, java.lang.String[] search, java.lang.String[] replace)
Replace all occurrences of a substring within a string with another string.static java.lang.String
replace(java.lang.String str, java.lang.String search, java.lang.String replace)
Replace all occurrences of a substring within a string with another string.static int
search(java.lang.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
search(java.lang.String str, java.lang.String keyw)
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(java.lang.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
searchIgnoreCase(java.lang.String str, java.lang.String keyw)
Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.static java.lang.String[]
split(java.lang.String str, char delim)
Returns an array of strings separated by the delimiter string.static java.lang.String[]
split(java.lang.String str, char delim, int size)
Returns an array of strings separated by the delimiter string.static java.lang.String[]
split(java.lang.String str, java.lang.String delim)
Returns an array of strings separated by the delimiter string.static java.lang.String[]
split(java.lang.String str, java.lang.String delim, int size)
Returns an array of strings separated by the delimiter string.static java.lang.String[]
splitCommaDelimitedString(java.lang.String str)
Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.static boolean
startsWith(java.lang.String str, char prefix)
Test if the givenString
starts with the specified prefix character.static boolean
startsWithIgnoreCase(java.lang.String str, java.lang.String prefix)
Test if the givenString
starts with the specified prefix, ignoring upper/lower case.static java.lang.String
toDelimitedString(java.lang.Object[] arr, java.lang.String delim)
Convert aString
array into a delimitedString
(e.g.static java.lang.String
toDelimitedString(java.util.Collection<?> list, java.lang.String delim)
Convert aCollection
into a delimitedString
(e.g.static java.lang.String[]
tokenize(java.lang.String str, java.lang.String delimiters)
Tokenize the givenString
into a String array via a StringTokenizer.static java.lang.String[]
tokenize(java.lang.String str, java.lang.String delimiters, boolean trim)
Tokenize the givenString
into aString
array via aStringTokenizer
.static java.lang.String
toLanguageTag(java.util.Locale locale)
Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.static java.lang.String
toLineDelimitedString(java.lang.Object[] arr)
Convert aString
array into a delimitedString
by a system-dependent line separator.static java.lang.String
toLineDelimitedString(java.util.Collection<?> list)
Convert aCollection
into a delimitedString
by a system-dependent line separator.static java.lang.String
trimAllWhitespace(java.lang.String str)
Trim all whitespace from the givenString
: leading, trailing, and in between characters.static java.lang.String
trimLeadingCharacter(java.lang.String str, char leadingCharacter)
Trim all occurrences of the supplied leading character from the givenString
.static java.lang.String
trimLeadingWhitespace(java.lang.String str)
Trim leading whitespace from the givenString
.static java.lang.String
trimTrailingCharacter(java.lang.String str, char trailingCharacter)
Trim all occurrences of the supplied trailing character from the givenString
.static java.lang.String
trimTrailingWhitespace(java.lang.String str)
Trim trailing whitespace from the givenString
.static java.lang.String
trimWhitespace(java.lang.String str)
Trim leading and trailing whitespace from the givenString
.
-
-
-
Field Detail
-
EMPTY
public static final java.lang.String EMPTY
The emptyString
- See Also:
- Constant Field Values
-
-
Method Detail
-
hasLength
public static boolean hasLength(java.lang.CharSequence chars)
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:
hasText(String)
-
hasLength
public static boolean hasLength(java.lang.String str)
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:
hasLength(CharSequence)
,hasText(String)
-
hasText
public static boolean hasText(java.lang.CharSequence str)
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:
str
- 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:
Character.isWhitespace(char)
-
hasText
public static boolean hasText(java.lang.String str)
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:
hasText(CharSequence)
-
containsWhitespace
public static boolean containsWhitespace(java.lang.CharSequence str)
Check whether the givenCharSequence
contains any whitespace characters.- Parameters:
str
- theCharSequence
to check (may benull
)- Returns:
true
if theCharSequence
is not empty and contains at least 1 whitespace character- See Also:
Character.isWhitespace(char)
-
containsWhitespace
public static boolean containsWhitespace(java.lang.String str)
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:
containsWhitespace(CharSequence)
-
trimWhitespace
public static java.lang.String trimWhitespace(java.lang.String str)
Trim leading and trailing whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
Character.isWhitespace(char)
-
trimAllWhitespace
public static java.lang.String trimAllWhitespace(java.lang.String str)
Trim all whitespace from the givenString
: leading, trailing, and in between characters.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
Character.isWhitespace(char)
-
trimLeadingWhitespace
public static java.lang.String trimLeadingWhitespace(java.lang.String str)
Trim leading whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
Character.isWhitespace(char)
-
trimTrailingWhitespace
public static java.lang.String trimTrailingWhitespace(java.lang.String str)
Trim trailing whitespace from the givenString
.- Parameters:
str
- theString
to check- Returns:
- the trimmed
String
- See Also:
Character.isWhitespace(char)
-
trimLeadingCharacter
public static java.lang.String trimLeadingCharacter(java.lang.String str, char leadingCharacter)
Trim all occurrences of the supplied leading character from the givenString
.- Parameters:
str
- theString
to checkleadingCharacter
- the leading character to be trimmed- Returns:
- the trimmed
String
-
trimTrailingCharacter
public static java.lang.String trimTrailingCharacter(java.lang.String str, char trailingCharacter)
Trim all occurrences of the supplied trailing character from the givenString
.- Parameters:
str
- theString
to checktrailingCharacter
- the trailing character to be trimmed- Returns:
- the trimmed
String
-
startsWithIgnoreCase
public static boolean startsWithIgnoreCase(java.lang.String str, java.lang.String prefix)
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:
String.startsWith(java.lang.String, int)
-
endsWithIgnoreCase
public static boolean endsWithIgnoreCase(java.lang.String str, java.lang.String suffix)
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:
String.endsWith(java.lang.String)
-
isEmpty
public static boolean isEmpty(java.lang.String str)
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
public static java.lang.String nullToEmpty(java.lang.String str)
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
public static java.lang.String emptyToNull(java.lang.String str)
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
-
startsWith
public static boolean startsWith(java.lang.String str, char prefix)
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:
String.startsWith(java.lang.String, int)
-
endsWith
public static boolean endsWith(java.lang.String str, char suffix)
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:
String.endsWith(java.lang.String)
-
replace
public static java.lang.String replace(java.lang.String str, java.lang.String search, java.lang.String replace)
Replace all occurrences of a substring within a string with another string.- Parameters:
str
-String
to examinesearch
-String
to replacereplace
-String
to insert- Returns:
- a
String
with the replacements
-
replace
public static java.lang.String replace(java.lang.String str, java.lang.String[] search, java.lang.String[] replace)
Replace all occurrences of a substring within a string with another string.- Parameters:
str
-String
to examinesearch
-String
array to replacereplace
-String
array to insert- Returns:
- a
String
with the replacements
-
split
public static java.lang.String[] split(java.lang.String str, java.lang.String delim)
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
public static java.lang.String[] split(java.lang.String str, java.lang.String delim, int size)
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
public static java.lang.String[] split(java.lang.String str, char delim)
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
public static java.lang.String[] split(java.lang.String str, char delim, int size)
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
public static int search(java.lang.String str, java.lang.String keyw)
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 stringkeyw
- the string to find- Returns:
- the number of times the specified string was found
-
searchIgnoreCase
public static int searchIgnoreCase(java.lang.String str, java.lang.String keyw)
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 stringkeyw
- the string to find- Returns:
- the number of times the specified string was found
-
search
public static int search(java.lang.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.- Parameters:
chars
- the target stringc
- the character to find- Returns:
- the number of times the specified character was found
-
searchIgnoreCase
public static int searchIgnoreCase(java.lang.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. When searching for the specified character, it is not case-sensitive.- Parameters:
chars
- the target stringc
- the character to find- Returns:
- the number of times the specified character was found
-
tokenize
public static java.lang.String[] tokenize(java.lang.String str, java.lang.String delimiters)
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
public static java.lang.String[] tokenize(java.lang.String str, java.lang.String delimiters, boolean trim)
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
public static java.lang.String toDelimitedString(java.lang.Object[] arr, java.lang.String delim)
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
public static java.lang.String toDelimitedString(java.util.Collection<?> list, java.lang.String delim)
Convert aCollection
into a delimitedString
(e.g. CSV).Useful for
toString()
implementations.- Parameters:
list
- the collectiondelim
- the delimiter to use (typically a ",")- Returns:
- the delimited
String
-
toLineDelimitedString
public static java.lang.String toLineDelimitedString(java.lang.Object[] arr)
Convert aString
array into a delimitedString
by a system-dependent line separator.- Parameters:
arr
- the array to display- Returns:
- the delimited
String
-
toLineDelimitedString
public static java.lang.String toLineDelimitedString(java.util.Collection<?> list)
Convert aCollection
into a delimitedString
by a system-dependent line separator.- Parameters:
list
- the collection- Returns:
- the delimited
String
-
splitCommaDelimitedString
public static java.lang.String[] splitCommaDelimitedString(java.lang.String str)
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
public static java.lang.String joinCommaDelimitedList(java.lang.String[] arr)
Convert aString
array into a comma delimitedString
(i.e., CSV).- Parameters:
arr
- the array to display- Returns:
- the delimited
String
-
joinCommaDelimitedList
public static java.lang.String joinCommaDelimitedList(java.util.Collection<?> list)
Convert aCollection
into a comma delimitedString
(i.e., CSV).- Parameters:
list
- the collection- Returns:
- the delimited
String
-
repeat
public static java.lang.String repeat(char ch, int 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
-
parseLocaleString
public static java.util.Locale parseLocaleString(java.lang.String localeString)
Parse the givenlocaleString
value into aLocale
.This is the inverse operation of
Locale's toString
.- Parameters:
localeString
- the localeString
, followingLocale's
toString()
format ("en", "en_UK", etc); also accepts spaces as separators, as an alternative to underscores- Returns:
- a corresponding
Locale
instance - Throws:
java.lang.IllegalArgumentException
- in case of an invalid locale specification
-
toLanguageTag
public static java.lang.String toLanguageTag(java.util.Locale locale)
Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.- Parameters:
locale
- the Locale to transform to a language tag- Returns:
- the RFC 3066 compliant language tag as
String
-
parseTimeZoneString
public static java.util.TimeZone parseTimeZoneString(java.lang.String timeZoneString)
Parse the giventimeZoneString
value into aTimeZone
.- Parameters:
timeZoneString
- the time zoneString
, followingTimeZone.getTimeZone(String)
but throwingIllegalArgumentException
in case of an invalid time zone specification- Returns:
- a corresponding
TimeZone
instance - Throws:
java.lang.IllegalArgumentException
- in case of an invalid time zone specification
-
convertToHumanFriendlyByteSize
public static java.lang.String convertToHumanFriendlyByteSize(long size)
Convert byte size into human friendly format.- Parameters:
size
- the number of bytes- Returns:
- a human friendly byte size (includes units)
-
convertToMachineFriendlyByteSize
public static long convertToMachineFriendlyByteSize(java.lang.String size)
Convert byte size into machine friendly format.- Parameters:
size
- the human friendly byte size (includes units)- Returns:
- a number of bytes
- Throws:
java.lang.NumberFormatException
- if failed parse given size
-
-