Class StringUtil


  • public final class StringUtil
    extends Object
    String utility class.
    • Method Detail

      • threadLocalStringBuilder

        public static StringBuilder threadLocalStringBuilder()
      • substringAfter

        public static String substringAfter​(String value,
                                            char delim)
        Get the item after one char delim if the delim is found (else null). This operation is a simplified and optimized version of String.split(String, int).
      • commonSuffixOfLength

        public static boolean commonSuffixOfLength​(String s,
                                                   String p,
                                                   int len)
        Checks if two strings have the same suffix of specified length
        Parameters:
        s - string
        p - string
        len - length of the common suffix
        Returns:
        true if both s and p are not null and both have the same suffix. Otherwise - false
      • byteToHexStringPadded

        public static String byteToHexStringPadded​(int value)
        Converts the specified byte value into a 2-digit hexadecimal integer.
      • byteToHexStringPadded

        public static <T extends Appendable> T byteToHexStringPadded​(T buf,
                                                                     int value)
        Converts the specified byte value into a 2-digit hexadecimal integer and appends it to the specified buffer.
      • toHexStringPadded

        public static String toHexStringPadded​(byte[] src)
        Converts the specified byte array into a hexadecimal value.
      • toHexStringPadded

        public static String toHexStringPadded​(byte[] src,
                                               int offset,
                                               int length)
        Converts the specified byte array into a hexadecimal value.
      • toHexStringPadded

        public static <T extends Appendable> T toHexStringPadded​(T dst,
                                                                 byte[] src)
        Converts the specified byte array into a hexadecimal value and appends it to the specified buffer.
      • toHexStringPadded

        public static <T extends Appendable> T toHexStringPadded​(T dst,
                                                                 byte[] src,
                                                                 int offset,
                                                                 int length)
        Converts the specified byte array into a hexadecimal value and appends it to the specified buffer.
      • byteToHexString

        public static String byteToHexString​(int value)
        Converts the specified byte value into a hexadecimal integer.
      • byteToHexString

        public static <T extends Appendable> T byteToHexString​(T buf,
                                                               int value)
        Converts the specified byte value into a hexadecimal integer and appends it to the specified buffer.
      • toHexString

        public static String toHexString​(byte[] src)
        Converts the specified byte array into a hexadecimal value.
      • toHexString

        public static String toHexString​(byte[] src,
                                         int offset,
                                         int length)
        Converts the specified byte array into a hexadecimal value.
      • toHexString

        public static <T extends Appendable> T toHexString​(T dst,
                                                           byte[] src)
        Converts the specified byte array into a hexadecimal value and appends it to the specified buffer.
      • toHexString

        public static <T extends Appendable> T toHexString​(T dst,
                                                           byte[] src,
                                                           int offset,
                                                           int length)
        Converts the specified byte array into a hexadecimal value and appends it to the specified buffer.
      • decodeHexNibble

        public static int decodeHexNibble​(char c)
        Helper to decode half of a hexadecimal number from a string.
        Parameters:
        c - The ASCII character of the hexadecimal number to decode. Must be in the range [0-9a-fA-F].
        Returns:
        The hexadecimal value represented in the ASCII character given, or -1 if the character is invalid.
      • decodeHexByte

        public static byte decodeHexByte​(CharSequence s,
                                         int pos)
        Decode a 2-digit hex byte from within a string.
      • decodeHexDump

        public static byte[] decodeHexDump​(CharSequence hexDump,
                                           int fromIndex,
                                           int length)
        Decodes part of a string with hex dump
        Parameters:
        hexDump - a CharSequence which contains the hex dump
        fromIndex - start of hex dump in hexDump
        length - hex string length
      • simpleClassName

        public static String simpleClassName​(Class<?> clazz)
        Generates a simplified name from a Class. Similar to Class.getSimpleName(), but it works fine with anonymous classes.
      • escapeCsv

        public static CharSequence escapeCsv​(CharSequence value)
        Escapes the specified value, if necessary according to RFC-4180.
        Parameters:
        value - The value which will be escaped according to RFC-4180
        Returns:
        CharSequence the escaped value if necessary, or the value unchanged
      • escapeCsv

        public static CharSequence escapeCsv​(CharSequence value,
                                             boolean trimWhiteSpace)
        Escapes the specified value, if necessary according to RFC-4180.
        Parameters:
        value - The value which will be escaped according to RFC-4180
        trimWhiteSpace - The value will first be trimmed of its optional white-space characters, according to RFC-7230
        Returns:
        CharSequence the escaped value if necessary, or the value unchanged
      • unescapeCsv

        public static CharSequence unescapeCsv​(CharSequence value)
        Unescapes the specified escaped CSV field, if necessary according to RFC-4180.
        Parameters:
        value - The escaped CSV field which will be unescaped according to RFC-4180
        Returns:
        CharSequence the unescaped value if necessary, or the value unchanged
      • unescapeCsvFields

        public static List<CharSequence> unescapeCsvFields​(CharSequence value)
        Unescapes the specified escaped CSV fields according to RFC-4180.
        Parameters:
        value - A string with multiple CSV escaped fields which will be unescaped according to RFC-4180
        Returns:
        List the list of unescaped fields
      • length

        public static int length​(String s)
        Get the length of a string, null input is considered 0 length.
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(String s)
        Determine if a string is null or String.isEmpty() returns true.
      • indexOfNonWhiteSpace

        public static int indexOfNonWhiteSpace​(CharSequence seq,
                                               int offset)
        Find the index of the first non-white space character in s starting at offset.
        Parameters:
        seq - The string to search.
        offset - The offset to start searching at.
        Returns:
        the index of the first non-white space character or <-1 if none was found.
      • indexOfWhiteSpace

        public static int indexOfWhiteSpace​(CharSequence seq,
                                            int offset)
        Find the index of the first white space character in s starting at offset.
        Parameters:
        seq - The string to search.
        offset - The offset to start searching at.
        Returns:
        the index of the first white space character or <-1 if none was found.
      • isSurrogate

        public static boolean isSurrogate​(char c)
        Determine if c lies within the range of values defined for Surrogate Code Point.
        Parameters:
        c - the character to check.
        Returns:
        true if c lies within the range of values defined for Surrogate Code Point. false otherwise.
      • endsWith

        public static boolean endsWith​(CharSequence s,
                                       char c)
        Determine if the string s ends with the char c.
        Parameters:
        s - the string to test
        c - the tested char
        Returns:
        true if s ends with the char c
      • trimOws

        public static CharSequence trimOws​(CharSequence value)
        Trim optional white-space characters from the specified value, according to RFC-7230.
        Parameters:
        value - the value to trim
        Returns:
        CharSequence the trimmed value if necessary, or the value unchanged
      • join

        public static CharSequence join​(CharSequence separator,
                                        Iterable<? extends CharSequence> elements)
        Returns a char sequence that contains all elements joined by a given separator.
        Parameters:
        separator - for each element
        elements - to join together
        Returns:
        a char sequence joined by a given separator.