Package com.aspectran.core.util
Class StringUtils
- java.lang.Object
-
- com.aspectran.core.util.StringUtils
-
public class StringUtils extends java.lang.ObjectStatic utility methods pertaining toStringorCharSequenceinstances.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringEMPTYThe emptyString
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsWhitespace(java.lang.CharSequence str)Check whether the givenCharSequencecontains any whitespace characters.static booleancontainsWhitespace(java.lang.String str)Check whether the givenStringcontains any whitespace characters.static java.lang.StringconvertToHumanFriendlyByteSize(long size)Convert byte size into human friendly format.static longconvertToMachineFriendlyByteSize(java.lang.String size)Convert byte size into machine friendly format.static java.lang.StringemptyToNull(java.lang.String str)Returns the given string if it is nonempty;nullotherwise.static booleanendsWith(java.lang.String str, char suffix)Test if the givenStringends with the specified prefix character.static booleanendsWithIgnoreCase(java.lang.String str, java.lang.String suffix)Test if the givenStringends with the specified suffix, ignoring upper/lower case.static booleanhasLength(java.lang.CharSequence chars)Check that the givenCharSequenceis neithernullnor of length 0.static booleanhasLength(java.lang.String str)Check that the givenStringis neithernullnor of length 0.static booleanhasText(java.lang.CharSequence str)Check whether the givenCharSequencecontains actual text.static booleanhasText(java.lang.String str)Check whether the givenStringcontains actual text.static booleanisEmpty(java.lang.String str)Returnstrueif the given string is null or is the empty string.static java.lang.StringjoinCommaDelimitedList(java.lang.String[] arr)Convert aStringarray into a comma delimitedString(i.e., CSV).static java.lang.StringjoinCommaDelimitedList(java.util.Collection<?> list)Convert aCollectioninto a comma delimitedString(i.e., CSV).static java.lang.StringnullToEmpty(java.lang.String str)Returns the given string if it is non-null; the empty string otherwise.static java.util.LocaleparseLocaleString(java.lang.String localeString)Parse the givenlocaleStringvalue into aLocale.static java.util.TimeZoneparseTimeZoneString(java.lang.String timeZoneString)Parse the giventimeZoneStringvalue into aTimeZone.static java.lang.Stringrepeat(char ch, int repeat)Returns padding using the specified delimiter repeated to a given length.static java.lang.Stringreplace(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.Stringreplace(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 intsearch(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 intsearch(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 intsearchIgnoreCase(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 intsearchIgnoreCase(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 booleanstartsWith(java.lang.String str, char prefix)Test if the givenStringstarts with the specified prefix character.static booleanstartsWithIgnoreCase(java.lang.String str, java.lang.String prefix)Test if the givenStringstarts with the specified prefix, ignoring upper/lower case.static java.lang.StringtoDelimitedString(java.lang.Object[] arr, java.lang.String delim)Convert aStringarray into a delimitedString(e.g.static java.lang.StringtoDelimitedString(java.util.Collection<?> list, java.lang.String delim)Convert aCollectioninto a delimitedString(e.g.static java.lang.String[]tokenize(java.lang.String str, java.lang.String delimiters)Tokenize the givenStringinto a String array via a StringTokenizer.static java.lang.String[]tokenize(java.lang.String str, java.lang.String delimiters, boolean trim)Tokenize the givenStringinto aStringarray via aStringTokenizer.static java.lang.StringtoLanguageTag(java.util.Locale locale)Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.static java.lang.StringtoLineDelimitedString(java.lang.Object[] arr)Convert aStringarray into a delimitedStringby a system-dependent line separator.static java.lang.StringtoLineDelimitedString(java.util.Collection<?> list)Convert aCollectioninto a delimitedStringby a system-dependent line separator.static java.lang.StringtrimAllWhitespace(java.lang.String str)Trim all whitespace from the givenString: leading, trailing, and in between characters.static java.lang.StringtrimLeadingCharacter(java.lang.String str, char leadingCharacter)Trim all occurrences of the supplied leading character from the givenString.static java.lang.StringtrimLeadingWhitespace(java.lang.String str)Trim leading whitespace from the givenString.static java.lang.StringtrimTrailingCharacter(java.lang.String str, char trailingCharacter)Trim all occurrences of the supplied trailing character from the givenString.static java.lang.StringtrimTrailingWhitespace(java.lang.String str)Trim trailing whitespace from the givenString.static java.lang.StringtrimWhitespace(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 givenCharSequenceis neithernullnor of length 0.Note: this method returns
truefor aCharSequencethat purely consists of whitespace.StringUtils.hasLength(null) = false StringUtils.hasLength("") = false StringUtils.hasLength(" ") = true StringUtils.hasLength("Hello") = true- Parameters:
chars- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis notnulland has length- See Also:
hasText(String)
-
hasLength
public static boolean hasLength(java.lang.String str)
Check that the givenStringis neithernullnor of length 0.Note: this method returns
truefor aStringthat purely consists of whitespace.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis notnulland has length- See Also:
hasLength(CharSequence),hasText(String)
-
hasText
public static boolean hasText(java.lang.CharSequence str)
Check whether the givenCharSequencecontains actual text.More specifically, this method returns
trueif theCharSequenceis 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- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis 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 givenStringcontains actual text.More specifically, this method returns
trueif theStringis notnull, its length is greater than 0, and it contains at least one non-whitespace character.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis 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 givenCharSequencecontains any whitespace characters.- Parameters:
str- theCharSequenceto check (may benull)- Returns:
trueif theCharSequenceis 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 givenStringcontains any whitespace characters.- Parameters:
str- theStringto check (may benull)- Returns:
trueif theStringis 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- theStringto 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- theStringto 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- theStringto 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- theStringto 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- theStringto 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- theStringto 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 givenStringstarts with the specified prefix, ignoring upper/lower case.- Parameters:
str- theStringto checkprefix- the prefix to look for- Returns:
trueif theStringstarts 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 givenStringends with the specified suffix, ignoring upper/lower case.- Parameters:
str- theStringto checksuffix- the suffix to look for- Returns:
trueif theStringends with the suffix, case insensitive, or bothnull- See Also:
String.endsWith(java.lang.String)
-
isEmpty
public static boolean isEmpty(java.lang.String str)
Returnstrueif the given string is null or is the empty string.- Parameters:
str- a string reference to check- Returns:
trueif 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:
stringitself 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;nullotherwise.- Parameters:
str- the string to test and possibly return- Returns:
stringitself if it is nonempty;nullif it is empty or null
-
startsWith
public static boolean startsWith(java.lang.String str, char prefix)Test if the givenStringstarts with the specified prefix character.- Parameters:
str- theStringto 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 givenStringends with the specified prefix character.- Parameters:
str- theStringto 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-Stringto examinesearch-Stringto replacereplace-Stringto insert- Returns:
- a
Stringwith 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-Stringto examinesearch-Stringarray to replacereplace-Stringarray to insert- Returns:
- a
Stringwith 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 givenStringinto a String array via a StringTokenizer.- Parameters:
str- theStringto 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 givenStringinto aStringarray 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 aStringarray 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 aCollectioninto 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 aStringarray into a delimitedStringby 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 aCollectioninto a delimitedStringby 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 aStringarray 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 aCollectioninto 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 givenlocaleStringvalue into aLocale.This is the inverse operation of
Locale's toString.- Parameters:
localeString- the localeString, followingLocale'stoString()format ("en", "en_UK", etc); also accepts spaces as separators, as an alternative to underscores- Returns:
- a corresponding
Localeinstance - 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 giventimeZoneStringvalue into aTimeZone.- Parameters:
timeZoneString- the time zoneString, followingTimeZone.getTimeZone(String)but throwingIllegalArgumentExceptionin case of an invalid time zone specification- Returns:
- a corresponding
TimeZoneinstance - 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
-
-