Class StringUtils
- Since:
- 1.0
- Author:
- Graeme Rocher
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
capitalize
(String str) Capitalizes the first character of the provided string.static String
convertDotToUnderscore
(String dottedProperty) Replace the dots in the property with underscore and transform to uppercase.static String
convertDotToUnderscore
(String dottedProperty, boolean uppercase) Replace the dots in the property with underscore and transform to uppercase based on given flag.static boolean
hasText
(@Nullable CharSequence str) Return whether the given string has non whitespace characters.internListOf
(Object... objects) Converts the given objects into a set of interned strings contained within an internal pool of sets.internMapOf
(Object... values) Converts the given objects into a map of interned strings.static boolean
Is the given string a series of digits.static boolean
isEmpty
(@Nullable CharSequence str) Return whether the given string is empty.static boolean
Return whether the given string is not empty.static boolean
Is the boolean string true.parseLocale
(String localeValue) Parse the givenString
value into aLocale
, accepting theLocale.toString()
format as well as BCP 47 language tags.static String
prependUri
(String baseUri, String uri) Prepends a partial uri and normalizes / characters.splitOmitEmptyStrings
(CharSequence sequence, char splitCharacter) Fast split by one character iterable.splitOmitEmptyStringsIterator
(CharSequence sequence, char splitCharacter) Fast split by one character iterator.splitOmitEmptyStringsList
(CharSequence sequence, char splitCharacter) Fast split by one character iterable.static String[]
tokenizeToStringArray
(String str, String delimiters) Tokenize the given String into a String array via a StringTokenizer.static String[]
tokenizeToStringArray
(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the given String into a String array via a StringTokenizer.static String
trimLeading
(String str, Predicate<Character> predicate) Returns a new string without any leading characters that match the supplied predicate.static String
trimLeadingCharacter
(String str, char c) Returns a new string without any leading characters that match the supplied character.static String
Returns a new string without any leading whitespace.trimToNull
(@Nullable String string) Trims the supplied string.
-
Field Details
-
TRUE
Constant for the value true.- See Also:
-
FALSE
Constant for the value false.- See Also:
-
EMPTY_STRING_ARRAY
Constant for an empty String array. -
EMPTY_STRING
Constant for an empty String.- See Also:
-
SPACE
public static final char SPACEa space.- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
isEmpty
Return whether the given string is empty.- Parameters:
str
- The string- Returns:
- True if str is empty or null
-
isNotEmpty
Return whether the given string is not empty.- Parameters:
str
- The string- Returns:
- True if str is not null and not empty
-
hasText
Return whether the given string has non whitespace characters.- Parameters:
str
- The string- Returns:
- True if str contains any non whitespace characters
-
internListOf
Converts the given objects into a set of interned strings contained within an internal pool of sets. SeeString.intern()
.- Parameters:
objects
- The objects- Returns:
- An unmodifiable, pooled set of strings
-
internMapOf
Converts the given objects into a map of interned strings. SeeString.intern()
.- Parameters:
values
- The objects- Returns:
- An unmodifiable set of strings
- See Also:
-
isDigits
Is the given string a series of digits.- Parameters:
str
- The string- Returns:
- True if it is a series of digits
-
parseLocale
Parse the givenString
value into aLocale
, accepting theLocale.toString()
format as well as BCP 47 language tags.- Parameters:
localeValue
- the locale value: following eitherLocale's
toString()
format ("en", "en_UK", etc.), also accepting spaces as separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK") as specified byLocale.forLanguageTag(java.lang.String)
on Java 7+Copied from the Spring Framework while retaining all license, copyright and author information.
- Returns:
- a corresponding
Locale
instance, ornull
if none - Throws:
IllegalArgumentException
- in case of an invalid locale specification- Since:
- 2.3.0
- See Also:
-
trimLeadingWhitespace
Returns a new string without any leading whitespace.- Parameters:
str
- The string- Returns:
- The string without leading whitespace
- Since:
- 2.3.0
-
trimLeadingCharacter
Returns a new string without any leading characters that match the supplied character.- Parameters:
str
- The stringc
- The character to remove- Returns:
- The string without leading characters matching the supplied character.
- Since:
- 2.3.0
-
trimLeading
Returns a new string without any leading characters that match the supplied predicate.- Parameters:
str
- The stringpredicate
- The predicate to test characters against- Returns:
- The string without leading characters matching the supplied predicate.
- Since:
- 2.3.0
-
tokenizeToStringArray
Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using
delimitedListToStringArray
Copied from the Spring Framework while retaining all license, copyright and author information.
- Parameters:
str
- the String to tokenizedelimiters
- the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).- Returns:
- an array of the tokens
- See Also:
-
tokenizeToStringArray
public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) Tokenize the given String into a String array via a StringTokenizer.The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using
delimitedListToStringArray
Copied from the Spring Framework while retaining all license, copyright and author information.
- Parameters:
str
- the String to tokenizedelimiters
- the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)trimTokens
- trim the tokens via String'strim
ignoreEmptyTokens
- omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).- Returns:
- an array of the tokens (
null
if the input String wasnull
) - See Also:
-
convertDotToUnderscore
Replace the dots in the property with underscore and transform to uppercase.- Parameters:
dottedProperty
- The property with dots, example - a.b.c- Returns:
- The converted value
-
convertDotToUnderscore
Replace the dots in the property with underscore and transform to uppercase based on given flag.- Parameters:
dottedProperty
- The property with dots, example - a.b.cuppercase
- To transform to uppercase string- Returns:
- The converted value
-
prependUri
Prepends a partial uri and normalizes / characters. For example, if the base uri is "/foo/" and the uri is "/bar/", the output will be "/foo/bar/". Similarly, if the base uri is "/foo" and the uri is "bar", the output will be "/foo/bar"- Parameters:
baseUri
- The uri to prepend. E.g. /foouri
- The uri to combine with the baseUri. E.g. /bar- Returns:
- A combined uri string
-
capitalize
Capitalizes the first character of the provided string.- Parameters:
str
- The string to capitalize- Returns:
- The capitalized string
-
trimToNull
Trims the supplied string. If the string is empty or null before or after trimming, null is returned.- Parameters:
string
- the string to trim- Returns:
- the trimmed string or null
-
isTrue
Is the boolean string true. Values that represent true are: yes, y, on, and true.- Parameters:
booleanString
- The boolean string- Returns:
- True if it is a valid value
-
splitOmitEmptyStrings
Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative toString.split(String)
where it's used with one character.- Parameters:
sequence
- The sequence to splitsplitCharacter
- The split character- Returns:
- The iterable of possible sequences
- Since:
- 2.5.0
-
splitOmitEmptyStringsList
Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative toString.split(String)
where it's used with one character.- Parameters:
sequence
- The sequence to splitsplitCharacter
- The split character- Returns:
- The list of possible sequences
- Since:
- 2.5.0
-
splitOmitEmptyStringsIterator
public static Iterator<String> splitOmitEmptyStringsIterator(CharSequence sequence, char splitCharacter) Fast split by one character iterator. Implementation omits empty strings. This should be a fast alternative toString.split(String)
where it's used with one character.- Parameters:
sequence
- The sequence to splitsplitCharacter
- The split character- Returns:
- The iterator of possible sequences
- Since:
- 2.5.0
-