Class TextUtils

java.lang.Object
org.apache.hc.core5.util.TextUtils

public final class TextUtils extends Object
Tests and converts Strings and CharSequence.
Since:
4.3
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final char[]
    Returns a hexadecimal string with lowercase letters, representing the values of the bytes.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte
    castAsByte(int c)
    Casts character to byte filtering non-visible and non-ASCII characters before conversion.
    static boolean
    Tests whether a CharSequence contains any whitespace.
    static boolean
    Determines whether the given CharSequence contains only ASCII characters.
    static boolean
    Checks if a CharSequence is empty (""), null or whitespace only.
    static boolean
    Tests whether the parameter is null or of zero length.
    static boolean
    isHex(char c)
    Tests whether the given character is an ASCII hexadecimal digit.
    static boolean
    isPrintable(int c)
    Tests whether a character can be written to an HTTP protocol line without replacing it.
    static int
    Gets a CharSequence length or 0 if the CharSequence is null.
    static String
    toHexString(byte[] bytes)
     
    static String
    Converts a String to its lower case representation using Locale.ROOT.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEX_CHARS

      private static final char[] HEX_CHARS
      Returns a hexadecimal string with lowercase letters, representing the values of the bytes.
      Since:
      5.0
  • Constructor Details

    • TextUtils

      private TextUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(CharSequence s)
      Tests whether the parameter is null or of zero length.
      Parameters:
      s - The CharSequence to test.
      Returns:
      whether the parameter is null or of zero length.
    • isBlank

      public static boolean isBlank(CharSequence s)

      Checks if a CharSequence is empty (""), null or whitespace only.

      Whitespace is defined by Character.isWhitespace(char).

      TextUtils.isBlank(null)      = true
      TextUtils.isBlank("")        = true
      TextUtils.isBlank(" ")       = true
      TextUtils.isBlank("abg")     = false
      TextUtils.isBlank("  abg  ") = false
      
      Parameters:
      s - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace only
    • length

      public static int length(CharSequence cs)
      Gets a CharSequence length or 0 if the CharSequence is null.
      Parameters:
      cs - a CharSequence or null
      Returns:
      CharSequence length or 0 if the CharSequence is null.
      Since:
      5.1
    • containsBlanks

      public static boolean containsBlanks(CharSequence s)
      Tests whether a CharSequence contains any whitespace.
      Parameters:
      s - The CharSequence to test.
      Returns:
      whether a CharSequence contains any whitespace.
      Since:
      4.4
    • toHexString

      public static String toHexString(byte[] bytes)
    • toLowerCase

      public static String toLowerCase(String s)
      Converts a String to its lower case representation using Locale.ROOT.
      Parameters:
      s - The String to convert
      Returns:
      The converted String.
      Since:
      5.2
    • isAllASCII

      public static boolean isAllASCII(CharSequence s)
      Determines whether the given CharSequence contains only ASCII characters.
      Parameters:
      s - the CharSequence to check
      Returns:
      true if the CharSequence contains only ASCII characters, false otherwise
      Throws:
      IllegalArgumentException - if the input CharSequence is null
      Since:
      5.3
    • castAsByte

      @Internal public static byte castAsByte(int c)
      Casts character to byte filtering non-visible and non-ASCII characters before conversion.
      Parameters:
      c - The character to cast.
      Returns:
      The given character or '?'.
      Since:
      5.2
    • isHex

      public static boolean isHex(char c)
      Tests whether the given character is an ASCII hexadecimal digit.

      Accepts '0'..'9', 'A'..'F', and 'a'..'f' only. This method does not consider non-ASCII numerals or fullwidth forms.

      Parameters:
      c - the character to test
      Returns:
      true if c is an ASCII hex digit, false otherwise
      Since:
      5.4
    • isPrintable

      @Internal public static boolean isPrintable(int c)
      Tests whether a character can be written to an HTTP protocol line without replacing it. Horizontal tab is permitted for compatibility.
      Parameters:
      c - The character to test.
      Returns:
      true if the character does not need filtering.
      Since:
      5.5