java.lang.Object
org.htmlunit.util.StringUtils
String utility class for 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 aColorparsed from the given RGB hexadecimal notation, ornullif the token is not a valid hex color.static booleancontainsIgnoreCase(String s, String expected) Returnstrueif the given string contains the specified substring, ignoring case.static booleancontainsOnly(CharSequence cs, char... valid) Returnstrueif the given sequence is non-nulland contains only characters from the provided set of valid characters.static StringcssCamelize(String string) Transforms a delimiter-separated CSS property name (e.g.static <T extends CharSequence>
TdefaultIfEmptyOrNull(T s, T defaultString) Returns the given sequence, or the default value if it is empty ornull.static booleanendsWithIgnoreCase(String s, String expectedEnd) Returnstrueif the given string ends with the specified suffix, ignoring case.static booleanequalsChar(char expected, CharSequence s) Returnstrueif the given sequence has exactly one character and it equals the expected char.static StringEscapes a string for use as XML 1.0 content, replacing",&,',<, and>with their XML entity equivalents.static StringescapeXmlAttributeValue(String attValue) Escapes a string for use as an XML attribute value.static StringEscapes the characters<,>and&into their XML entity equivalents.static ColorfindColorHSL(String token) Returns aColorparsed from the firsthsl(...)notation found within the token, ornullif none is found.static ColorfindColorRGB(String token) Returns aColorparsed from the firstrgb(...)notation found within the token, ornullif none is found.static ColorfindColorRGBA(String token) Returns aColorparsed from the firstrgba(...)notation found within the token, ornullif none is found.static StringformatColor(Color color) Formats the specified color as anrgb(...)string.static intReturns the index within the specified string of the first occurrence of the specified search character within the given range.static booleanReturnstrueif the given sequence isnull, empty, or contains only whitespace.static booleanReturnstrueif the given sequence isnullor empty.static booleanReturnstrueif the given sequence is notnulland is empty.static booleanReturnstrueif the given sequence is notnull, not empty, and contains at least one non-whitespace character.static StringreplaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a string in a single pass.static StringsanitizeForAppendReplacement(String toSanitize) Sanitizes a string for use withMatcher.appendReplacement(java.lang.StringBuffer, java.lang.String).static StringsanitizeForFileName(String toSanitize) Sanitizes a string for use as a filename by replacing illegal characters (\,/,|,:,?,*,",<,>, control characters) with_.static String[]splitAtBlank(String str) Splits the given text on blank (space) characters.static String[]splitAtComma(String str) Splits the given text on comma characters.static String[]Splits the given text on comma or blank (space) characters.static String[]Splits the given text on whitespace as defined byCharacter.isWhitespace(char).static booleanstartsWithIgnoreCase(String s, String expectedStart) Returnstrueif the given string starts with the specified prefix, ignoring case.static StringsubstringAfter(String str, String find) Returns the substring after the first occurrence of the specified separator.static StringsubstringBefore(String str, String find) Returns the substring before the first occurrence of the specified separator.static byte[]toByteArray(String content, Charset charset) Converts the given string to a byte array using the specified charset.static floatParses a float from the given string, returning the default value if parsing fails or the string isnull.static intParses an integer from the given string, returning the default value if parsing fails or the string isnull.static StringConverts the given string to lowercase using the ROOT locale.static StringStrips trailing whitespace from the given string.
-
Field Details
-
EMPTY_STRING
The empty String"".- See Also:
-
-
Method Details
-
isEmptyString
Returnstrueif the given sequence is notnulland is empty. UnlikeStringUtils.isEmpty(CharSequence), this returnsfalseif the sequence isnull.- Parameters:
s- the string to check- Returns:
trueif the string is notnulland has length 0
-
isEmptyOrNull
Returnstrueif the given sequence isnullor empty.- Parameters:
s- the string to check- Returns:
trueif the string isnullor has length 0
-
defaultIfEmptyOrNull
Returns the given sequence, or the default value if it is empty ornull.- Type Parameters:
T- the kind ofCharSequence- Parameters:
s- the sequence to checkdefaultString- the default value to return if the input is empty ornull- Returns:
- the given sequence, or
defaultString
-
isBlank
Returnstrueif the given sequence isnull, empty, or contains only whitespace.- Parameters:
s- the sequence to check- Returns:
trueif the sequence is blank
-
isNotBlank
Returnstrueif the given sequence is notnull, not empty, and contains at least one non-whitespace character.- Parameters:
s- the sequence to check- Returns:
trueif the sequence is not blank
-
equalsChar
Returnstrueif the given sequence has exactly one character and it equals the expected char.- Parameters:
expected- the char to compares- the string to check- Returns:
trueif the string has exactly one character matchingexpected
-
startsWithIgnoreCase
Returnstrueif the given string starts with the specified prefix, ignoring case.- Parameters:
s- the string to checkexpectedStart- the expected prefix (must not benullor empty)- Returns:
trueif the string starts with the given prefix, ignoring case
-
endsWithIgnoreCase
Returnstrueif the given string ends with the specified suffix, ignoring case.- Parameters:
s- the string to checkexpectedEnd- the expected suffix (must not benullor empty)- Returns:
trueif the string ends with the given suffix, ignoring case
-
containsIgnoreCase
Returnstrueif the given string contains the specified substring, ignoring case.- Parameters:
s- the string to checkexpected- the substring to look for (must not benullor empty)- Returns:
trueif the string contains the substring, ignoring case
-
replaceChars
Replaces multiple characters in a string in a single pass. This method can also be used to delete characters by omitting them fromreplaceChars.- Parameters:
str- the string to process; may benullsearchChars- the set of characters to search for; may benullreplaceChars- the replacement characters; may benull- Returns:
- the modified string, or the original if no replacement was performed
-
substringAfter
Returns the substring after the first occurrence of the specified separator. The separator itself is not included.Returns
nullfor anullinput string. Returns an empty string for an empty input string. Returns an empty string if the separator is not found. Returns an empty string if the separator isnulland the input is notnull.- Parameters:
str- the string to search; may benullfind- the separator to find; may benull- Returns:
- the substring after the first occurrence of the separator, or
nullif input isnull
-
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
Escapes a string for use as XML 1.0 content, replacing",&,',<, and>with their XML entity equivalents. Invalid XML 1.0 code points are removed.- Parameters:
text- the text to escape- Returns:
- the escaped value, or
nullif the input isnull
-
escapeXmlAttributeValue
Escapes a string for use as an XML attribute value. Only<,&, and"are escaped, as required by the XML specification. Invalid XML 1.0 code points are removed.- Parameters:
attValue- the attribute value to escape- Returns:
- the escaped value, or
nullif the input isnull
-
indexOf
Returns the index within the specified string of the first occurrence of the specified search character within the given range.- 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 (exclusive)- Returns:
- the index of the first occurrence, or
-1if not found
-
asColorHexadecimal
Returns aColorparsed from the given RGB hexadecimal notation, ornullif the token is not a valid hex color.- Parameters:
token- the token to parse- Returns:
- a
Colorif the token is a valid hex color; otherwisenull
-
findColorRGB
Returns aColorparsed from the firstrgb(...)notation found within the token, ornullif none is found.- Parameters:
token- the token to parse- Returns:
- a
Colorif an RGB color is found; otherwisenull
-
findColorRGBA
Returns aColorparsed from the firstrgba(...)notation found within the token, ornullif none is found.- Parameters:
token- the token to parse- Returns:
- a
Colorif an RGBA color is found; otherwisenull
-
findColorHSL
Returns aColorparsed from the firsthsl(...)notation found within the token, ornullif none is found.- Parameters:
token- the token to parse- Returns:
- a
Colorif an HSL color is found; otherwisenull
-
formatColor
Formats the specified color as anrgb(...)string.- Parameters:
color- the color to format- Returns:
- the specified color formatted as
rgb(r, g, b)
-
sanitizeForAppendReplacement
Sanitizes a string for use withMatcher.appendReplacement(java.lang.StringBuffer, java.lang.String). Replaces all\with\\and$with\$, as these are control characters inappendReplacement.- Parameters:
toSanitize- the string to sanitize- Returns:
- the sanitized version of the string
-
sanitizeForFileName
Sanitizes a string for use as a filename by replacing illegal characters (\,/,|,:,?,*,",<,>, control characters) with_.- Parameters:
toSanitize- the string to sanitize- Returns:
- the sanitized version of the string
-
cssCamelize
Transforms a delimiter-separated CSS property name (e.g.font-size) into camel case (e.g.fontSize).- Parameters:
string- the string to camelize- Returns:
- the camelized string
-
toRootLowerCase
Converts the given string to lowercase using the ROOT locale. Returnsnullif the input isnull.- Parameters:
s- the string to lowercase- Returns:
- the lowercased string, or
null
-
toByteArray
Converts the given string to a byte array using the specified charset. Returns an empty byte array if the string isnullor empty, or if the charset is unsupported.- Parameters:
charset- the charset to use for encodingcontent- the string to convert- Returns:
- the string as a byte array, or an empty array if the string is
nullor empty
-
splitAtJavaWhitespace
Splits the given text on whitespace as defined byCharacter.isWhitespace(char).- Parameters:
str- the string to split; may benull- Returns:
- an array of parsed strings, or an empty array if the input is
null
-
splitAtBlank
Splits the given text on blank (space) characters.- Parameters:
str- the string to split; may benull- Returns:
- an array of parsed strings, or an empty array if the input is
null
-
splitAtComma
Splits the given text on comma characters.- Parameters:
str- the string to split; may benull- Returns:
- an array of parsed strings, or an empty array if the input is
null
-
splitAtCommaOrBlank
Splits the given text on comma or blank (space) characters.- Parameters:
str- the string to split; may benull- Returns:
- an array of parsed strings, or an empty array if the input is
null
-
substringBefore
Returns the substring before the first occurrence of the specified separator. The separator itself is not included in the result.nullinput returnsnull; empty input returns an empty string.- Parameters:
str- the string to search; may benullfind- the separator to find; must not benullor empty- Returns:
- the substring before the first occurrence of the separator, or the full string if the separator is not found
-
toInt
Parses an integer from the given string, returning the default value if parsing fails or the string isnull.- Parameters:
str- the string to parse; may benulldefaultValue- the value to return if parsing fails- Returns:
- the parsed integer, or
defaultValue
-
toFloat
Parses a float from the given string, returning the default value if parsing fails or the string isnull.- Parameters:
str- the string to parse; may benulldefaultValue- the value to return if parsing fails- Returns:
- the parsed float, or
defaultValue
-
trimRight
Strips trailing whitespace from the given string. Returnsnullfor anullinput; returns the string unchanged if it has no trailing whitespace.- Parameters:
str- the string to trim; may benull- Returns:
- the trimmed string, or
nullif input isnull
-
containsOnly
Returnstrueif the given sequence is non-nulland contains only characters from the provided set of valid characters.- Parameters:
cs- the sequence to check; may benullvalid- the array of valid characters; must not benullor empty- Returns:
trueif all characters in the sequence are valid
-