Module org.htmlunit

Class StringUtils

java.lang.Object
org.htmlunit.util.StringUtils

public final class StringUtils extends Object
String utility class for functions not covered by third-party libraries.
Author:
Daniel Gredler, Ahmed Ashour, Martin Tamme, Ronald Brill
  • Field Details

  • Method Details

    • isEmptyString

      public static boolean isEmptyString(CharSequence s)
      Returns true if the given sequence is not null and is empty. Unlike StringUtils.isEmpty(CharSequence), this returns false if the sequence is null.
      Parameters:
      s - the string to check
      Returns:
      true if the string is not null and has length 0
    • isEmptyOrNull

      public static boolean isEmptyOrNull(CharSequence s)
      Returns true if the given sequence is null or empty.
      Parameters:
      s - the string to check
      Returns:
      true if the string is null or has length 0
    • defaultIfEmptyOrNull

      public static <T extends CharSequence> T defaultIfEmptyOrNull(T s, T defaultString)
      Returns the given sequence, or the default value if it is empty or null.
      Type Parameters:
      T - the kind of CharSequence
      Parameters:
      s - the sequence to check
      defaultString - the default value to return if the input is empty or null
      Returns:
      the given sequence, or defaultString
    • isBlank

      public static boolean isBlank(CharSequence s)
      Returns true if the given sequence is null, empty, or contains only whitespace.
      Parameters:
      s - the sequence to check
      Returns:
      true if the sequence is blank
    • isNotBlank

      public static boolean isNotBlank(CharSequence s)
      Returns true if the given sequence is not null, not empty, and contains at least one non-whitespace character.
      Parameters:
      s - the sequence to check
      Returns:
      true if the sequence is not blank
    • equalsChar

      public static boolean equalsChar(char expected, CharSequence s)
      Returns true if the given sequence has exactly one character and it equals the expected char.
      Parameters:
      expected - the char to compare
      s - the string to check
      Returns:
      true if the string has exactly one character matching expected
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String s, String expectedStart)
      Returns true if the given string starts with the specified prefix, ignoring case.
      Parameters:
      s - the string to check
      expectedStart - the expected prefix (must not be null or empty)
      Returns:
      true if the string starts with the given prefix, ignoring case
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String s, String expectedEnd)
      Returns true if the given string ends with the specified suffix, ignoring case.
      Parameters:
      s - the string to check
      expectedEnd - the expected suffix (must not be null or empty)
      Returns:
      true if the string ends with the given suffix, ignoring case
    • containsIgnoreCase

      public static boolean containsIgnoreCase(String s, String expected)
      Returns true if the given string contains the specified substring, ignoring case.
      Parameters:
      s - the string to check
      expected - the substring to look for (must not be null or empty)
      Returns:
      true if the string contains the substring, ignoring case
    • replaceChars

      public static String replaceChars(String str, String searchChars, String replaceChars)
      Replaces multiple characters in a string in a single pass. This method can also be used to delete characters by omitting them from replaceChars.
      Parameters:
      str - the string to process; may be null
      searchChars - the set of characters to search for; may be null
      replaceChars - the replacement characters; may be null
      Returns:
      the modified string, or the original if no replacement was performed
    • substringAfter

      public static String substringAfter(String str, String find)
      Returns the substring after the first occurrence of the specified separator. The separator itself is not included.

      Returns null for a null input 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 is null and the input is not null.

      Parameters:
      str - the string to search; may be null
      find - the separator to find; may be null
      Returns:
      the substring after the first occurrence of the separator, or null if input is null
    • escapeXmlChars

      public static String escapeXmlChars(String s)
      Escapes the characters <, > and & into their XML entity equivalents.
      Parameters:
      s - the string to escape
      Returns:
      the escaped form of the specified string
    • escapeXml

      public static String escapeXml(String text)
      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 null if the input is null
    • escapeXmlAttributeValue

      public static String escapeXmlAttributeValue(String attValue)
      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 null if the input is null
    • indexOf

      public static int indexOf(String s, char searchChar, int beginIndex, int endIndex)
      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 search
      searchChar - the character to search for
      beginIndex - the index at which to start the search
      endIndex - the index at which to stop the search (exclusive)
      Returns:
      the index of the first occurrence, or -1 if not found
    • asColorHexadecimal

      public static Color asColorHexadecimal(String token)
      Returns a Color parsed from the given RGB hexadecimal notation, or null if the token is not a valid hex color.
      Parameters:
      token - the token to parse
      Returns:
      a Color if the token is a valid hex color; otherwise null
    • findColorRGB

      public static Color findColorRGB(String token)
      Returns a Color parsed from the first rgb(...) notation found within the token, or null if none is found.
      Parameters:
      token - the token to parse
      Returns:
      a Color if an RGB color is found; otherwise null
    • findColorRGBA

      public static Color findColorRGBA(String token)
      Returns a Color parsed from the first rgba(...) notation found within the token, or null if none is found.
      Parameters:
      token - the token to parse
      Returns:
      a Color if an RGBA color is found; otherwise null
    • findColorHSL

      public static Color findColorHSL(String token)
      Returns a Color parsed from the first hsl(...) notation found within the token, or null if none is found.
      Parameters:
      token - the token to parse
      Returns:
      a Color if an HSL color is found; otherwise null
    • formatColor

      public static String formatColor(Color color)
      Formats the specified color as an rgb(...) string.
      Parameters:
      color - the color to format
      Returns:
      the specified color formatted as rgb(r, g, b)
    • sanitizeForAppendReplacement

      public static String sanitizeForAppendReplacement(String toSanitize)
      Sanitizes a string for use with Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String). Replaces all \ with \\ and $ with \$, as these are control characters in appendReplacement.
      Parameters:
      toSanitize - the string to sanitize
      Returns:
      the sanitized version of the string
    • sanitizeForFileName

      public static String sanitizeForFileName(String toSanitize)
      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

      public static String cssCamelize(String string)
      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

      public static String toRootLowerCase(String s)
      Converts the given string to lowercase using the ROOT locale. Returns null if the input is null.
      Parameters:
      s - the string to lowercase
      Returns:
      the lowercased string, or null
    • toByteArray

      public static byte[] toByteArray(String content, Charset charset)
      Converts the given string to a byte array using the specified charset. Returns an empty byte array if the string is null or empty, or if the charset is unsupported.
      Parameters:
      charset - the charset to use for encoding
      content - the string to convert
      Returns:
      the string as a byte array, or an empty array if the string is null or empty
    • splitAtJavaWhitespace

      public static String[] splitAtJavaWhitespace(String str)
      Splits the given text on whitespace as defined by Character.isWhitespace(char).
      Parameters:
      str - the string to split; may be null
      Returns:
      an array of parsed strings, or an empty array if the input is null
    • splitAtBlank

      public static String[] splitAtBlank(String str)
      Splits the given text on blank (space) characters.
      Parameters:
      str - the string to split; may be null
      Returns:
      an array of parsed strings, or an empty array if the input is null
    • splitAtComma

      public static String[] splitAtComma(String str)
      Splits the given text on comma characters.
      Parameters:
      str - the string to split; may be null
      Returns:
      an array of parsed strings, or an empty array if the input is null
    • splitAtCommaOrBlank

      public static String[] splitAtCommaOrBlank(String str)
      Splits the given text on comma or blank (space) characters.
      Parameters:
      str - the string to split; may be null
      Returns:
      an array of parsed strings, or an empty array if the input is null
    • substringBefore

      public static String substringBefore(String str, String find)
      Returns the substring before the first occurrence of the specified separator. The separator itself is not included in the result. null input returns null; empty input returns an empty string.
      Parameters:
      str - the string to search; may be null
      find - the separator to find; must not be null or empty
      Returns:
      the substring before the first occurrence of the separator, or the full string if the separator is not found
    • toInt

      public static int toInt(String str, int defaultValue)
      Parses an integer from the given string, returning the default value if parsing fails or the string is null.
      Parameters:
      str - the string to parse; may be null
      defaultValue - the value to return if parsing fails
      Returns:
      the parsed integer, or defaultValue
    • toFloat

      public static float toFloat(String str, float defaultValue)
      Parses a float from the given string, returning the default value if parsing fails or the string is null.
      Parameters:
      str - the string to parse; may be null
      defaultValue - the value to return if parsing fails
      Returns:
      the parsed float, or defaultValue
    • trimRight

      public static String trimRight(String str)
      Strips trailing whitespace from the given string. Returns null for a null input; returns the string unchanged if it has no trailing whitespace.
      Parameters:
      str - the string to trim; may be null
      Returns:
      the trimmed string, or null if input is null
    • containsOnly

      public static boolean containsOnly(CharSequence cs, char... valid)
      Returns true if the given sequence is non-null and contains only characters from the provided set of valid characters.
      Parameters:
      cs - the sequence to check; may be null
      valid - the array of valid characters; must not be null or empty
      Returns:
      true if all characters in the sequence are valid