Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Fast String Utilities. These string utilities provide both convenience methods and performance improvements over most standard library versions. The main aim of the optimizations is to avoid object creation unless absolutely required.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String __ISO_8859_1  
      static java.lang.String __LINE_SEPARATOR
      Deprecated.
      use System.lineSeparator() instead
      static java.lang.String __UTF16  
      static java.lang.String __UTF8  
      static java.lang.String ALL_INTERFACES  
      static java.lang.String CRLF  
      static char[] lowercases  
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void append​(java.lang.StringBuilder buf, byte b, int base)
      append hex digit
      static void append​(java.lang.StringBuilder buf, java.lang.String s, int offset, int length)
      Append substring to StringBuilder
      static void append2digits​(java.lang.StringBuffer buf, int i)
      Append 2 digits (zero padded) to the StringBuffer
      static void append2digits​(java.lang.StringBuilder buf, int i)
      Append 2 digits (zero padded) to the StringBuilder
      static java.lang.String[] arrayFromString​(java.lang.String s)
      Parse the string representation of a list using csvSplit(List, String, int, int)
      static java.lang.String asciiToLowerCase​(java.lang.String s)
      fast lower case conversion.
      static java.lang.String asciiToUpperCase​(java.lang.String s)
      fast upper case conversion.
      static java.lang.String[] csvSplit​(java.lang.String s)
      Parse a CSV string using csvSplit(List, String, int, int)
      static java.lang.String[] csvSplit​(java.lang.String s, int off, int len)
      Parse a CSV string using csvSplit(List, String, int, int)
      static java.util.List<java.lang.String> csvSplit​(java.util.List<java.lang.String> list, java.lang.String s, int off, int len)
      Split a quoted comma separated string to a list
      static boolean endsWithIgnoreCase​(java.lang.String s, java.lang.String w)  
      static boolean equals​(java.lang.String s, char[] buf, int offset, int length)  
      static byte[] getBytes​(java.lang.String s)  
      static byte[] getBytes​(java.lang.String s, java.lang.String charset)  
      static byte[] getUtf8Bytes​(java.lang.String s)  
      static int indexFrom​(java.lang.String s, java.lang.String chars)
      returns the next index of a character from the chars string
      static int indexOfControlChars​(java.lang.String str)
      Find the index of a control characters in String
      static boolean isBlank​(java.lang.String str)
      Test if a string is null or only has whitespace characters in it.
      static boolean isEmpty​(java.lang.String str)
      Checks if a String is empty ("") or null.
      static boolean isHex​(java.lang.String str, int offset, int length)  
      static boolean isNotBlank​(java.lang.String str)
      Test if a string is not null and contains at least 1 non-whitespace characters in it.
      static boolean isUTF8​(java.lang.String charset)  
      static java.lang.String nonNull​(java.lang.String s)
      Return a non null string.
      static java.lang.String normalizeCharset​(java.lang.String s)
      Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
      static java.lang.String normalizeCharset​(java.lang.String s, int offset, int length)
      Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
      static java.lang.String printable​(byte[] b)  
      static java.lang.String printable​(java.lang.String name)  
      static java.lang.String replace​(java.lang.String str, char find, char with)
      Replace chars within string.
      static java.lang.String replace​(java.lang.String s, java.lang.String sub, java.lang.String with)
      Replace substrings within string.
      static java.lang.String replaceFirst​(java.lang.String original, java.lang.String target, java.lang.String replacement)
      Replace first substrings within string.
      static java.lang.String sanitizeFileSystemName​(java.lang.String str)
      Replace all characters from input string that are known to have special meaning in various filesystems.
      static java.lang.String sanitizeXmlString​(java.lang.String html)  
      static java.lang.String sidBytesToString​(byte[] sidBytes)
      Deprecated.
      static byte[] sidStringToBytes​(java.lang.String sidString)
      Deprecated.
      static boolean startsWithIgnoreCase​(java.lang.String s, java.lang.String w)  
      static java.lang.String stringFrom​(java.lang.String s, int n)
      Generate a string from another string repeated n times.
      static java.lang.String strip​(java.lang.String str, java.lang.String find)  
      static int toInt​(java.lang.String string, int from)
      Convert String to an integer.
      static long toLong​(java.lang.String string)
      Convert String to an long.
      static java.lang.String toString​(byte[] b, int offset, int length, java.lang.String charset)  
      static java.lang.String toUTF8String​(byte[] b, int offset, int length)  
      static java.lang.String truncate​(java.lang.String str, int maxSize)
      Truncate a string to a max size.
      static java.lang.String unquote​(java.lang.String s)
      Deprecated.
      static java.lang.String valueOf​(java.lang.Object object)
      The String value of an Object
      • Methods inherited from class java.lang.Object

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

      • __LINE_SEPARATOR

        @Deprecated
        public static final java.lang.String __LINE_SEPARATOR
        Deprecated.
        use System.lineSeparator() instead
      • lowercases

        public static final char[] lowercases
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • normalizeCharset

        public static java.lang.String normalizeCharset​(java.lang.String s)
        Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
        Parameters:
        s - the charset to normalize
        Returns:
        the normalized charset (or null if normalized version not found)
      • normalizeCharset

        public static java.lang.String normalizeCharset​(java.lang.String s,
                                                        int offset,
                                                        int length)
        Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
        Parameters:
        s - the charset to normalize
        offset - the offset in the charset
        length - the length of the charset in the input param
        Returns:
        the normalized charset (or null if not found)
      • asciiToLowerCase

        public static java.lang.String asciiToLowerCase​(java.lang.String s)
        fast lower case conversion. Only works on ascii (not unicode)
        Parameters:
        s - the string to convert
        Returns:
        a lower case version of s
      • asciiToUpperCase

        public static java.lang.String asciiToUpperCase​(java.lang.String s)
        fast upper case conversion. Only works on ascii (not unicode)
        Parameters:
        s - the string to convert
        Returns:
        a lower case version of s
      • sanitizeFileSystemName

        public static java.lang.String sanitizeFileSystemName​(java.lang.String str)
        Replace all characters from input string that are known to have special meaning in various filesystems.

        This will replace all of the following characters with a "_" (underscore).

        • Control Characters
        • Anything not 7-bit printable ASCII
        • Special characters: pipe, redirect, combine, slash, equivalence, bang, glob, selection, etc...
        • Space
        Parameters:
        str - the raw input string
        Returns:
        the sanitized output string. or null if str is null.
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(java.lang.String s,
                                                   java.lang.String w)
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(java.lang.String s,
                                                 java.lang.String w)
      • indexFrom

        public static int indexFrom​(java.lang.String s,
                                    java.lang.String chars)
        returns the next index of a character from the chars string
        Parameters:
        s - the input string to search
        chars - the chars to look for
        Returns:
        the index of the character in the input stream found.
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               char find,
                                               char with)
        Replace chars within string.

        Fast replacement for java.lang.String#String.replace(char, char)

        Parameters:
        str - the input string
        find - the char to look for
        with - the char to replace with
        Returns:
        the now replaced string
      • replace

        public static java.lang.String replace​(java.lang.String s,
                                               java.lang.String sub,
                                               java.lang.String with)
        Replace substrings within string.

        Fast replacement for java.lang.String#String.replace(CharSequence, CharSequence)

        Parameters:
        s - the input string
        sub - the string to look for
        with - the string to replace with
        Returns:
        the now replaced string
      • replaceFirst

        public static java.lang.String replaceFirst​(java.lang.String original,
                                                    java.lang.String target,
                                                    java.lang.String replacement)
        Replace first substrings within string.

        Fast replacement for java.lang.String#String.replaceFirst(String, String), but without Regex support.

        Parameters:
        original - the original string
        target - the target string to look for
        replacement - the replacement string to use
        Returns:
        the replaced string
      • unquote

        @Deprecated
        public static java.lang.String unquote​(java.lang.String s)
        Deprecated.
        Remove single or double quotes.
        Parameters:
        s - the input string
        Returns:
        the string with quotes removed
      • append

        public static void append​(java.lang.StringBuilder buf,
                                  java.lang.String s,
                                  int offset,
                                  int length)
        Append substring to StringBuilder
        Parameters:
        buf - StringBuilder to append to
        s - String to append from
        offset - The offset of the substring
        length - The length of the substring
      • append

        public static void append​(java.lang.StringBuilder buf,
                                  byte b,
                                  int base)
        append hex digit
        Parameters:
        buf - the buffer to append to
        b - the byte to append
        base - the base of the hex output (almost always 16).
      • append2digits

        public static void append2digits​(java.lang.StringBuffer buf,
                                         int i)
        Append 2 digits (zero padded) to the StringBuffer
        Parameters:
        buf - the buffer to append to
        i - the value to append
      • append2digits

        public static void append2digits​(java.lang.StringBuilder buf,
                                         int i)
        Append 2 digits (zero padded) to the StringBuilder
        Parameters:
        buf - the buffer to append to
        i - the value to append
      • stringFrom

        public static java.lang.String stringFrom​(java.lang.String s,
                                                  int n)
        Generate a string from another string repeated n times.
        Parameters:
        s - the string to use
        n - the number of times this string should be appended
      • nonNull

        public static java.lang.String nonNull​(java.lang.String s)
        Return a non null string.
        Parameters:
        s - String
        Returns:
        The string passed in or empty string if it is null.
      • equals

        public static boolean equals​(java.lang.String s,
                                     char[] buf,
                                     int offset,
                                     int length)
      • toUTF8String

        public static java.lang.String toUTF8String​(byte[] b,
                                                    int offset,
                                                    int length)
      • toString

        public static java.lang.String toString​(byte[] b,
                                                int offset,
                                                int length,
                                                java.lang.String charset)
      • indexOfControlChars

        public static int indexOfControlChars​(java.lang.String str)
        Find the index of a control characters in String

        This will return a result on the first occurrence of a control character, regardless if there are more than one.

        Note: uses codepoint version of Character.isISOControl(int) to support Unicode better.

           indexOfControlChars(null)      == -1
           indexOfControlChars("")        == -1
           indexOfControlChars("\r\n")    == 0
           indexOfControlChars("\t")      == 0
           indexOfControlChars("   ")     == -1
           indexOfControlChars("a")       == -1
           indexOfControlChars(".")       == -1
           indexOfControlChars(";\n")     == 1
           indexOfControlChars("abc\f")   == 3
           indexOfControlChars("z\010")   == 1
           indexOfControlChars(":") == 1
         
        Parameters:
        str - the string to test.
        Returns:
        the index of first control character in string, -1 if no control characters encountered
      • isBlank

        public static boolean isBlank​(java.lang.String str)
        Test if a string is null or only has whitespace characters in it.

        Note: uses codepoint version of Character.isWhitespace(int) to support Unicode better.

           isBlank(null)   == true
           isBlank("")     == true
           isBlank("\r\n") == true
           isBlank("\t")   == true
           isBlank("   ")  == true
           isBlank("a")    == false
           isBlank(".")    == false
           isBlank(";\n")  == false
         
        Parameters:
        str - the string to test.
        Returns:
        true if string is null or only whitespace characters, false if non-whitespace characters encountered.
      • isEmpty

        public static boolean isEmpty​(java.lang.String str)

        Checks if a String is empty ("") or null.

           isEmpty(null)   == true
           isEmpty("")     == true
           isEmpty("\r\n") == false
           isEmpty("\t")   == false
           isEmpty("   ")  == false
           isEmpty("a")    == false
           isEmpty(".")    == false
           isEmpty(";\n")  == false
         
        Parameters:
        str - the string to test.
        Returns:
        true if string is null or empty.
      • isNotBlank

        public static boolean isNotBlank​(java.lang.String str)
        Test if a string is not null and contains at least 1 non-whitespace characters in it.

        Note: uses codepoint version of Character.isWhitespace(int) to support Unicode better.

           isNotBlank(null)   == false
           isNotBlank("")     == false
           isNotBlank("\r\n") == false
           isNotBlank("\t")   == false
           isNotBlank("   ")  == false
           isNotBlank("a")    == true
           isNotBlank(".")    == true
           isNotBlank(";\n")  == true
         
        Parameters:
        str - the string to test.
        Returns:
        true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters.
      • isUTF8

        public static boolean isUTF8​(java.lang.String charset)
      • isHex

        public static boolean isHex​(java.lang.String str,
                                    int offset,
                                    int length)
      • printable

        public static java.lang.String printable​(java.lang.String name)
      • printable

        public static java.lang.String printable​(byte[] b)
      • getBytes

        public static byte[] getBytes​(java.lang.String s)
      • getUtf8Bytes

        public static byte[] getUtf8Bytes​(java.lang.String s)
      • getBytes

        public static byte[] getBytes​(java.lang.String s,
                                      java.lang.String charset)
      • sidBytesToString

        @Deprecated
        public static java.lang.String sidBytesToString​(byte[] sidBytes)
        Deprecated.
        Converts a binary SID to a string SID http://en.wikipedia.org/wiki/Security_Identifier S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn
        Parameters:
        sidBytes - the SID bytes to build from
        Returns:
        the string SID
      • sidStringToBytes

        @Deprecated
        public static byte[] sidStringToBytes​(java.lang.String sidString)
        Deprecated.
        Converts a string SID to a binary SID http://en.wikipedia.org/wiki/Security_Identifier S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn
        Parameters:
        sidString - the string SID
        Returns:
        the binary SID
      • toInt

        public static int toInt​(java.lang.String string,
                                int from)
        Convert String to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
        Parameters:
        string - A String containing an integer.
        from - The index to start parsing from
        Returns:
        an int
      • toLong

        public static long toLong​(java.lang.String string)
        Convert String to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
        Parameters:
        string - A String containing an integer.
        Returns:
        an int
      • truncate

        public static java.lang.String truncate​(java.lang.String str,
                                                int maxSize)
        Truncate a string to a max size.
        Parameters:
        str - the string to possibly truncate
        maxSize - the maximum size of the string
        Returns:
        the truncated string. if str param is null, then the returned string will also be null.
      • arrayFromString

        public static java.lang.String[] arrayFromString​(java.lang.String s)
        Parse the string representation of a list using csvSplit(List, String, int, int)
        Parameters:
        s - The string to parse, expected to be enclosed as '[...]'
        Returns:
        An array of parsed values.
      • csvSplit

        public static java.lang.String[] csvSplit​(java.lang.String s)
        Parse a CSV string using csvSplit(List, String, int, int)
        Parameters:
        s - The string to parse
        Returns:
        An array of parsed values.
      • csvSplit

        public static java.lang.String[] csvSplit​(java.lang.String s,
                                                  int off,
                                                  int len)
        Parse a CSV string using csvSplit(List, String, int, int)
        Parameters:
        s - The string to parse
        off - The offset into the string to start parsing
        len - The len in characters to parse
        Returns:
        An array of parsed values.
      • csvSplit

        public static java.util.List<java.lang.String> csvSplit​(java.util.List<java.lang.String> list,
                                                                java.lang.String s,
                                                                int off,
                                                                int len)
        Split a quoted comma separated string to a list

        Handle rfc4180-like CSV strings, with the exceptions:

        • quoted values may contain double quotes escaped with back-slash
        • Non-quoted values are trimmed of leading trailing white space
        • trailing commas are ignored
        • double commas result in a empty string value
        Parameters:
        list - The Collection to split to (or null to get a new list)
        s - The string to parse
        off - The offset into the string to start parsing
        len - The len in characters to parse
        Returns:
        list containing the parsed list values
      • sanitizeXmlString

        public static java.lang.String sanitizeXmlString​(java.lang.String html)
      • strip

        public static java.lang.String strip​(java.lang.String str,
                                             java.lang.String find)
      • valueOf

        public static java.lang.String valueOf​(java.lang.Object object)
        The String value of an Object

        This method calls String.valueOf(Object) unless the object is null, in which case null is returned

        Parameters:
        object - The object
        Returns:
        String value or null