java.lang.Object
org.htmlunit.util.StringUtils
String utilities class for utility functions not covered by third party libraries.
- Author:
- Daniel Gredler, Ahmed Ashour, Martin Tamme, Ronald Brill
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic ColorasColorHexadecimal(String token) Returns a Color parsed from the given RGB in hexadecimal notation.static booleancontainsIgnoreCase(String s, String expected) Tests if a CharSequence ends with a specified prefix.static booleancontainsOnly(CharSequence cs, char... valid) static StringcssCamelize(String string) Transforms the specified string from delimiter-separated (e.g.static <T extends CharSequence>
TdefaultIfEmptyOrNull(T s, T defaultString) Returns either the passed in CharSequence, or if the CharSequence is empty ornull, the default value.static booleanendsWithIgnoreCase(String s, String expectedEnd) Tests if a CharSequence ends with a specified prefix.static booleanequalsChar(char expected, CharSequence s) static StringEscape the string to be used as xml 1.0 content be replacing the characters '"', '&', ''', '<', and '>' into their XML entity equivalents.static StringescapeXmlAttributeValue(String attValue) Escape the string to be used as attribute value.static StringEscapes the characters '<', '>' and '&' into their XML entity equivalents.static ColorfindColorHSL(String token) Returns a Color parsed from the given hsl notation if found inside the given string.static ColorfindColorRGB(String token) Returns a Color parsed from the given rgb notation if found inside the given string.static ColorfindColorRGBA(String token) Returns a Color parsed from the given rgb notation.static StringformatColor(Color color) Formats the specified color.static intReturns the index within the specified string of the first occurrence of the specified search character.static booleanTests if a CharSequence is null, empty, or contains only whitespace.static booleanReturns true if the param is null or empty.static booleanReturns true if the param is not null and empty.static booleanTests if a CharSequence is NOT null, empty, or contains only whitespace.static StringreplaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a String in one go.static StringsanitizeForAppendReplacement(String toSanitize) Sanitize a string for use in Matcher.appendReplacement.static StringsanitizeForFileName(String toSanitize) Sanitizes a string for use as filename.static String[]splitAtBlank(String str) Splits the provided text into an array, using blank as the separator.static String[]splitAtComma(String str) Splits the provided text into an array, using blank as the separator.static String[]Splits the provided text into an array, using comma or blank as the separator.static String[]Splits the provided text into an array, using whitespace as the separator.static booleanstartsWithIgnoreCase(String s, String expectedStart) Tests if a CharSequence starts with a specified prefix.static StringsubstringAfter(String str, String find) Gets the substring after the first occurrence of a separator.static StringsubstringBefore(String str, String find) Gets the substring before the first occurrence of a separator.static byte[]toByteArray(String content, Charset charset) Converts a string into a byte array using the specified encoding.static floatTries to converts aStringinto anfloat, returning a default value if the conversion fails.static intTries to converts aStringinto anint, returning a default value if the conversion fails.static StringLowercases a string by checking and check for null first.static StringStrips any whitespace from the end of a String.
-
Field Details
-
EMPTY_STRING
The empty String"".- See Also:
-
-
Method Details
-
isEmptyString
Returns true if the param is not null and empty. This is different fromStringUtils.isEmpty(CharSequence)because this returns false if the provided string is null.- Parameters:
s- the string to check- Returns:
- true if the param is not null and empty
-
isEmptyOrNull
Returns true if the param is null or empty.- Parameters:
s- the string to check- Returns:
- true if the param is null or empty
-
defaultIfEmptyOrNull
Returns either the passed in CharSequence, or if the CharSequence is empty ornull, the default value.- Type Parameters:
T- the kind of CharSequence- Parameters:
s- the CharSequence to checkdefaultString- the default to return if the input is empty or null- Returns:
- the passed in CharSequence, or the defaultString
-
isBlank
Tests if a CharSequence is null, empty, or contains only whitespace.- Parameters:
s- the CharSequence to check- Returns:
- true if a CharSequence is null, empty, or contains only whitespace
-
isNotBlank
Tests if a CharSequence is NOT null, empty, or contains only whitespace.- Parameters:
s- the CharSequence to check- Returns:
- false if a CharSequence is null, empty, or contains only whitespace
-
equalsChar
- Parameters:
expected- the char that we expects- the string to check- Returns:
- true if the provided string has only one char and this matches the expectation
-
startsWithIgnoreCase
Tests if a CharSequence starts with a specified prefix.- Parameters:
s- the string to checkexpectedStart- the string that we expect at the beginning (has to be not null and not empty)- Returns:
- true if the provided string has only one char and this matches the expectation
-
endsWithIgnoreCase
Tests if a CharSequence ends with a specified prefix.- Parameters:
s- the string to checkexpectedEnd- the string that we expect at the end (has to be not null and not empty)- Returns:
- true if the provided string has only one char and this matches the expectation
-
containsIgnoreCase
Tests if a CharSequence ends with a specified prefix.- Parameters:
s- the string to checkexpected- the string that we expect to be a substring (has to be not null and not empty)- Returns:
- true if the provided string has only one char and this matches the expectation
-
replaceChars
Replaces multiple characters in a String in one go. This method can also be used to delete characters.- Parameters:
str- String to replace characters in, may be null.searchChars- a set of characters to search for, may be null.replaceChars- a set of characters to replace, may be null.- Returns:
- modified String, or the input string if no replace was done.
-
substringAfter
Gets the substring after the first occurrence of a separator. The separator is not returned.A
nullstring input will returnnull. An empty ("") string input will return the empty string. Anullseparator will return the empty string if the input string is notnull.If nothing is found, the empty string is returned.
- Parameters:
str- the String to get a substring from, may be null.find- the String to find, may be null.- Returns:
- the substring after the first occurrence of the specified string,
nullif null String input.
-
escapeXmlChars
Escapes the characters '<', '>' and '&' into their XML entity equivalents.- Parameters:
s- the string to escape- Returns:
- the escaped form of the specified string
-
escapeXml
Escape the string to be used as xml 1.0 content be replacing the characters '"', '&', ''', '<', and '>' into their XML entity equivalents.- Parameters:
text- the attribute value- Returns:
- the escaped value
-
escapeXmlAttributeValue
Escape the string to be used as attribute value. Only<,&and"have to be escaped (see http://www.w3.org/TR/REC-xml/#d0e888).- Parameters:
attValue- the attribute value- Returns:
- the escaped value
-
indexOf
Returns the index within the specified string of the first occurrence of the specified search character.- Parameters:
s- the string to searchsearchChar- the character to search forbeginIndex- the index at which to start the searchendIndex- the index at which to stop the search- Returns:
- the index of the first occurrence of the character in the string or
-1
-
asColorHexadecimal
Returns a Color parsed from the given RGB in hexadecimal notation.- Parameters:
token- the token to parse- Returns:
- a Color whether the token is a color RGB in hexadecimal notation; otherwise null
-
findColorRGB
Returns a Color parsed from the given rgb notation if found inside the given string.- Parameters:
token- the token to parse- Returns:
- a Color whether the token contains a color in RGB notation; otherwise null
-
findColorRGBA
Returns a Color parsed from the given rgb notation.- Parameters:
token- the token to parse- Returns:
- a Color whether the token is a color in RGB notation; otherwise null
-
findColorHSL
Returns a Color parsed from the given hsl notation if found inside the given string.- Parameters:
token- the token to parse- Returns:
- a Color whether the token contains a color in RGB notation; otherwise null
-
formatColor
Formats the specified color.- Parameters:
color- the color to format- Returns:
- the specified color, formatted
-
sanitizeForAppendReplacement
Sanitize a string for use in Matcher.appendReplacement. Replaces all \ with \\ and $ as \$ because they are used as control characters in appendReplacement.- Parameters:
toSanitize- the string to sanitize- Returns:
- sanitized version of the given string
-
sanitizeForFileName
Sanitizes a string for use as filename. Replaces \, /, |, :, ?, *, ", <, >, control chars by _ (underscore).- Parameters:
toSanitize- the string to sanitize- Returns:
- sanitized version of the given string
-
cssCamelize
Transforms the specified string from delimiter-separated (e.g.font-size) to camel-cased (e.g.fontSize).- Parameters:
string- the string to camelize- Returns:
- the transformed string
-
toRootLowerCase
Lowercases a string by checking and check for null first. There is no cache involved and the ROOT locale is used to convert it.- Parameters:
s- the string to lowercase- Returns:
- the lowercased string
-
toByteArray
Converts a string into a byte array using the specified encoding.- Parameters:
charset- the charsetcontent- the string to convert- Returns:
- the String as a byte[]; if the specified encoding is not supported an empty byte[] will be returned
-
splitAtJavaWhitespace
Splits the provided text into an array, using whitespace as the separator. Whitespace is defined byCharacter.isWhitespace(char).- Parameters:
str- the String to parse, may be null- Returns:
- an array of parsed Strings, an empty array if null String input
-
splitAtBlank
Splits the provided text into an array, using blank as the separator.- Parameters:
str- the String to parse, may be null- Returns:
- an array of parsed Strings, an empty array if null String input
-
splitAtComma
Splits the provided text into an array, using blank as the separator.- Parameters:
str- the String to parse, may be null- Returns:
- an array of parsed Strings, an empty array if null String input
-
splitAtCommaOrBlank
Splits the provided text into an array, using comma or blank as the separator.- Parameters:
str- the String to parse, may be null- Returns:
- an array of parsed Strings, an empty array if null String input
-
substringBefore
Gets the substring before the first occurrence of a separator. The separator is not returned. Anullstring input will returnnull. An empty ("") string input will return the empty string. Anullor empty separator is not allowed (will throw).- Parameters:
str- the String to get a substring from, may be null.find- the String to find, not null and not empty- Returns:
- the substring before the first occurrence of the specified string,
nullif null String input.
-
toInt
Tries to converts aStringinto anint, returning a default value if the conversion fails. If the string isnull, the default value is returned.- Parameters:
str- the string to convert, may be null.defaultValue- the default value.- Returns:
- the int represented by the string, or the default if conversion fails or the provides str is
null
-
toFloat
Tries to converts aStringinto anfloat, returning a default value if the conversion fails. If the string isnull, the default value is returned.- Parameters:
str- the string to convert, may be null.defaultValue- the default value.- Returns:
- the float represented by the string, or the default if conversion fails or the provides str is
null
-
trimRight
Strips any whitespace from the end of a String.A
nullinput String returnsnull. An empty string ("") input returns the empty string.- Parameters:
str- the String to remove characters from, may be null.- Returns:
- the stripped String,
nullif null String input.
-
containsOnly
- Parameters:
cs- the String to check, may be null.valid- an array of valid chars, may be null.- Returns:
- true if it only contains valid chars and is non-null.
-