Package org.h2.util

Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    A few String utility functions.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String addAsterisk​(java.lang.String s, int index)
      Add an asterisk ('[*]') at the given position.
      static java.lang.StringBuilder appendHex​(java.lang.StringBuilder builder, long x, int bytes)
      Appends specified number of trailing bytes from unsigned long value to a specified string builder.
      static java.lang.StringBuilder appendTwoDigits​(java.lang.StringBuilder builder, int positiveValue)
      Append a zero-padded number from 00 to 99 to a string builder.
      static java.lang.StringBuilder appendZeroPadded​(java.lang.StringBuilder builder, int length, long positiveValue)
      Append a zero-padded number to a string builder.
      static java.lang.String arrayCombine​(java.lang.String[] list, char separatorChar)
      Combine an array of strings to one array using the given separator character.
      static java.lang.String[] arraySplit​(java.lang.String s, char separatorChar, boolean trim)
      Split a string into an array of strings using the given separator.
      static java.lang.String cache​(java.lang.String s)
      Get the string from the cache if possible.
      static void clearCache()
      Clear the cache.
      static char[] cloneCharArray​(char[] chars)
      Create a new char array and copy all the data.
      static java.lang.String convertBytesToHex​(byte[] value)
      Convert a byte array to a hex encoded string.
      static java.lang.String convertBytesToHex​(byte[] value, int len)
      Convert a byte array to a hex encoded string.
      static java.lang.StringBuilder convertBytesToHex​(java.lang.StringBuilder builder, byte[] value)
      Convert a byte array to a hex encoded string and appends it to a specified string builder.
      static java.lang.StringBuilder convertBytesToHex​(java.lang.StringBuilder builder, byte[] value, int len)
      Convert a byte array to a hex encoded string and appends it to a specified string builder.
      static byte[] convertHexToBytes​(java.lang.String s)
      Convert a hex encoded string to a byte array.
      static java.io.ByteArrayOutputStream convertHexWithSpacesToBytes​(java.io.ByteArrayOutputStream baos, java.lang.String s, int start, int end)
      Parses a hex encoded string with possible space separators and appends the decoded binary string to the specified output stream.
      static java.lang.String decodeUnicodeStringSQL​(java.lang.String s, int uencode)
      Decodes a Unicode SQL string.
      static java.lang.String escapeMetaDataPattern​(java.lang.String pattern)
      Escape table or schema patterns used for DatabaseMetaData functions.
      private static java.lang.String[] getCache()  
      private static DbException getFormatException​(java.lang.String s, int i)  
      private static DbException getHexStringException​(int code, java.lang.String s, int start, int end)  
      static java.lang.StringBuilder indent​(java.lang.StringBuilder builder, java.lang.String s, int spaces, boolean newline)
      Indents a string with spaces and appends it to a specified builder.
      static boolean isNullOrEmpty​(java.lang.String s)
      Check if a String is null or empty (the length is null).
      static boolean isNumber​(java.lang.String s)
      Check if this string is a decimal number.
      static boolean isWhitespaceOrEmpty​(java.lang.String s)
      Check if the specified string is empty or contains only whitespace.
      static java.lang.String javaDecode​(java.lang.String s)
      Decode a text that is encoded as a Java string literal.
      static java.lang.String javaEncode​(java.lang.String s)
      Convert a string to a Java literal using the correct escape sequences.
      static void javaEncode​(java.lang.String s, java.lang.StringBuilder buff, boolean forSQL)
      Convert a string to a Java literal using the correct escape sequences.
      static java.lang.String pad​(java.lang.String string, int n, java.lang.String padding, boolean right)
      Pad a string.
      static int parseUInt31​(java.lang.String s, int start, int end)
      Parses an unsigned 31-bit integer.
      static java.lang.String quoteIdentifier​(java.lang.String s)
      Enclose a string with double quotes.
      static java.lang.StringBuilder quoteIdentifier​(java.lang.StringBuilder builder, java.lang.String s)
      Enclose a string with double quotes and append it to the specified string builder.
      private static java.lang.StringBuilder quoteIdentifierOrLiteral​(java.lang.StringBuilder builder, java.lang.String s, char q)  
      static java.lang.String quoteJavaIntArray​(int[] array)
      Convert an int array to the Java source code that represents this array.
      static java.lang.String quoteJavaString​(java.lang.String s)
      Convert a string to the Java literal and enclose it with double quotes.
      static java.lang.String quoteJavaStringArray​(java.lang.String[] array)
      Convert a string array to the Java source code that represents this array.
      static java.lang.String quoteStringSQL​(java.lang.String s)
      Convert a string to a SQL literal.
      static java.lang.StringBuilder quoteStringSQL​(java.lang.StringBuilder builder, java.lang.String s)
      Convert a string to a SQL character string literal.
      static java.lang.String replaceAll​(java.lang.String s, java.lang.String before, java.lang.String after)
      Replace all occurrences of the before string with the after string.
      static java.lang.String toLowerEnglish​(java.lang.String s)
      Convert a string to lowercase using the English locale.
      static java.lang.String toUpperEnglish​(java.lang.String s)
      Convert a string to uppercase using the English locale.
      static java.lang.String trim​(java.lang.String s, boolean leading, boolean trailing, java.lang.String sp)
      Trim a character from a string.
      static java.lang.StringBuilder trimSubstring​(java.lang.StringBuilder builder, java.lang.String s, int beginIndex, int endIndex)
      Trim a whitespace from a substring and append it to a specified string builder.
      static java.lang.String trimSubstring​(java.lang.String s, int beginIndex)
      Trim a whitespace from a substring.
      static java.lang.String trimSubstring​(java.lang.String s, int beginIndex, int endIndex)
      Trim a whitespace from a substring.
      static java.lang.String truncateString​(java.lang.String s, int maximumLength)
      Truncates the specified string to the specified length.
      static java.lang.String urlDecode​(java.lang.String encoded)
      Decode the URL to a string.
      static java.lang.String urlEncode​(java.lang.String s)
      Encode the string as a URL.
      static java.lang.String xmlAttr​(java.lang.String name, java.lang.String value)
      Creates an XML attribute of the form name="value".
      static java.lang.String xmlCData​(java.lang.String data)
      Converts the data to a CDATA element.
      static java.lang.String xmlComment​(java.lang.String data)
      Escapes a comment.
      static java.lang.String xmlNode​(java.lang.String name, java.lang.String attributes, java.lang.String content)
      Create an XML node with optional attributes and content.
      static java.lang.String xmlNode​(java.lang.String name, java.lang.String attributes, java.lang.String content, boolean indent)
      Create an XML node with optional attributes and content.
      static java.lang.String xmlStartDoc()
      Returns <?xml version="1.0"?>
      static java.lang.String xmlText​(java.lang.String text)
      Escapes an XML text element.
      static java.lang.String xmlText​(java.lang.String text, boolean escapeNewline)
      Escapes an XML text element.
      • Methods inherited from class java.lang.Object

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

      • softCache

        private static java.lang.ref.SoftReference<java.lang.String[]> softCache
      • softCacheCreatedNs

        private static long softCacheCreatedNs
      • HEX

        private static final char[] HEX
      • HEX_DECODE

        private static final int[] HEX_DECODE
      • TO_UPPER_CACHE_MAX_ENTRY_LENGTH

        private static final int TO_UPPER_CACHE_MAX_ENTRY_LENGTH
        See Also:
        Constant Field Values
      • TO_UPPER_CACHE

        private static final java.lang.String[][] TO_UPPER_CACHE
    • Constructor Detail

      • StringUtils

        private StringUtils()
    • Method Detail

      • getCache

        private static java.lang.String[] getCache()
      • toUpperEnglish

        public static java.lang.String toUpperEnglish​(java.lang.String s)
        Convert a string to uppercase using the English locale.
        Parameters:
        s - the test to convert
        Returns:
        the uppercase text
      • toLowerEnglish

        public static java.lang.String toLowerEnglish​(java.lang.String s)
        Convert a string to lowercase using the English locale.
        Parameters:
        s - the text to convert
        Returns:
        the lowercase text
      • quoteStringSQL

        public static java.lang.String quoteStringSQL​(java.lang.String s)
        Convert a string to a SQL literal. Null is converted to NULL. The text is enclosed in single quotes. If there are any special characters, the method STRINGDECODE is used.
        Parameters:
        s - the text to convert.
        Returns:
        the SQL literal
      • quoteStringSQL

        public static java.lang.StringBuilder quoteStringSQL​(java.lang.StringBuilder builder,
                                                             java.lang.String s)
        Convert a string to a SQL character string literal. Null is converted to NULL. If there are any special characters, the Unicode character string literal is used.
        Parameters:
        builder - string builder to append result to
        s - the text to convert
        Returns:
        the specified string builder
      • decodeUnicodeStringSQL

        public static java.lang.String decodeUnicodeStringSQL​(java.lang.String s,
                                                              int uencode)
        Decodes a Unicode SQL string.
        Parameters:
        s - the string to decode
        uencode - the code point of UENCODE character, or '\\'
        Returns:
        the decoded string
        Throws:
        DbException - on format exception
      • javaEncode

        public static java.lang.String javaEncode​(java.lang.String s)
        Convert a string to a Java literal using the correct escape sequences. The literal is not enclosed in double quotes. The result can be used in properties files or in Java source code.
        Parameters:
        s - the text to convert
        Returns:
        the Java representation
      • javaEncode

        public static void javaEncode​(java.lang.String s,
                                      java.lang.StringBuilder buff,
                                      boolean forSQL)
        Convert a string to a Java literal using the correct escape sequences. The literal is not enclosed in double quotes. The result can be used in properties files or in Java source code.
        Parameters:
        s - the text to convert
        buff - the Java representation to return
        forSQL - true if we embedding this inside a STRINGDECODE SQL command
      • addAsterisk

        public static java.lang.String addAsterisk​(java.lang.String s,
                                                   int index)
        Add an asterisk ('[*]') at the given position. This format is used to show where parsing failed in a statement.
        Parameters:
        s - the text
        index - the position
        Returns:
        the text with asterisk
      • getFormatException

        private static DbException getFormatException​(java.lang.String s,
                                                      int i)
      • javaDecode

        public static java.lang.String javaDecode​(java.lang.String s)
        Decode a text that is encoded as a Java string literal. The Java properties file format and Java source code format is supported.
        Parameters:
        s - the encoded string
        Returns:
        the string
      • quoteJavaString

        public static java.lang.String quoteJavaString​(java.lang.String s)
        Convert a string to the Java literal and enclose it with double quotes. Null will result in "null" (without double quotes).
        Parameters:
        s - the text to convert
        Returns:
        the Java representation
      • quoteJavaStringArray

        public static java.lang.String quoteJavaStringArray​(java.lang.String[] array)
        Convert a string array to the Java source code that represents this array. Null will be converted to 'null'.
        Parameters:
        array - the string array
        Returns:
        the Java source code (including new String[]{})
      • quoteJavaIntArray

        public static java.lang.String quoteJavaIntArray​(int[] array)
        Convert an int array to the Java source code that represents this array. Null will be converted to 'null'.
        Parameters:
        array - the int array
        Returns:
        the Java source code (including new int[]{})
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String s)
        Encode the string as a URL.
        Parameters:
        s - the string to encode
        Returns:
        the encoded string
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String encoded)
        Decode the URL to a string.
        Parameters:
        encoded - the encoded URL
        Returns:
        the decoded string
      • arraySplit

        public static java.lang.String[] arraySplit​(java.lang.String s,
                                                    char separatorChar,
                                                    boolean trim)
        Split a string into an array of strings using the given separator. A null string will result in a null array, and an empty string in a zero element array.
        Parameters:
        s - the string to split
        separatorChar - the separator character
        trim - whether each element should be trimmed
        Returns:
        the array list
      • arrayCombine

        public static java.lang.String arrayCombine​(java.lang.String[] list,
                                                    char separatorChar)
        Combine an array of strings to one array using the given separator character. A backslash and the separator character and escaped using a backslash.
        Parameters:
        list - the string array
        separatorChar - the separator character
        Returns:
        the combined string
      • xmlAttr

        public static java.lang.String xmlAttr​(java.lang.String name,
                                               java.lang.String value)
        Creates an XML attribute of the form name="value". A single space is prepended to the name, so that multiple attributes can be concatenated.
        Parameters:
        name - the attribute name
        value - the attribute value
        Returns:
        the attribute
      • xmlNode

        public static java.lang.String xmlNode​(java.lang.String name,
                                               java.lang.String attributes,
                                               java.lang.String content)
        Create an XML node with optional attributes and content. The data is indented with 4 spaces if it contains a newline character.
        Parameters:
        name - the element name
        attributes - the attributes (may be null)
        content - the content (may be null)
        Returns:
        the node
      • xmlNode

        public static java.lang.String xmlNode​(java.lang.String name,
                                               java.lang.String attributes,
                                               java.lang.String content,
                                               boolean indent)
        Create an XML node with optional attributes and content. The data is indented with 4 spaces if it contains a newline character and the indent parameter is set to true.
        Parameters:
        name - the element name
        attributes - the attributes (may be null)
        content - the content (may be null)
        indent - whether to indent the content if it contains a newline
        Returns:
        the node
      • indent

        public static java.lang.StringBuilder indent​(java.lang.StringBuilder builder,
                                                     java.lang.String s,
                                                     int spaces,
                                                     boolean newline)
        Indents a string with spaces and appends it to a specified builder.
        Parameters:
        builder - string builder to append to
        s - the string
        spaces - the number of spaces
        newline - append a newline if there is none
        Returns:
        the specified string builder
      • xmlComment

        public static java.lang.String xmlComment​(java.lang.String data)
        Escapes a comment. If the data contains '--', it is converted to '- -'. The data is indented with 4 spaces if it contains a newline character.
        Parameters:
        data - the comment text
        Returns:
        <!-- data -->
      • xmlCData

        public static java.lang.String xmlCData​(java.lang.String data)
        Converts the data to a CDATA element. If the data contains ']]>', it is escaped as a text element.
        Parameters:
        data - the text data
        Returns:
        <![CDATA[data]]>
      • xmlStartDoc

        public static java.lang.String xmlStartDoc()
        Returns <?xml version="1.0"?>
        Returns:
        <?xml version="1.0"?>
      • xmlText

        public static java.lang.String xmlText​(java.lang.String text)
        Escapes an XML text element.
        Parameters:
        text - the text data
        Returns:
        the escaped text
      • xmlText

        public static java.lang.String xmlText​(java.lang.String text,
                                               boolean escapeNewline)
        Escapes an XML text element.
        Parameters:
        text - the text data
        escapeNewline - whether to escape newlines
        Returns:
        the escaped text
      • replaceAll

        public static java.lang.String replaceAll​(java.lang.String s,
                                                  java.lang.String before,
                                                  java.lang.String after)
        Replace all occurrences of the before string with the after string. Unlike String.replaceAll(String, String) this method reads before and after arguments as plain strings and if before argument is an empty string this method returns original string s.
        Parameters:
        s - the string
        before - the old text
        after - the new text
        Returns:
        the string with the before string replaced
      • quoteIdentifier

        public static java.lang.String quoteIdentifier​(java.lang.String s)
        Enclose a string with double quotes. A double quote inside the string is escaped using a double quote.
        Parameters:
        s - the text
        Returns:
        the double quoted text
      • quoteIdentifier

        public static java.lang.StringBuilder quoteIdentifier​(java.lang.StringBuilder builder,
                                                              java.lang.String s)
        Enclose a string with double quotes and append it to the specified string builder. A double quote inside the string is escaped using a double quote.
        Parameters:
        builder - string builder to append to
        s - the text
        Returns:
        the specified builder
      • quoteIdentifierOrLiteral

        private static java.lang.StringBuilder quoteIdentifierOrLiteral​(java.lang.StringBuilder builder,
                                                                        java.lang.String s,
                                                                        char q)
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.String s)
        Check if a String is null or empty (the length is null).
        Parameters:
        s - the string to check
        Returns:
        true if it is null or empty
      • pad

        public static java.lang.String pad​(java.lang.String string,
                                           int n,
                                           java.lang.String padding,
                                           boolean right)
        Pad a string. This method is used for the SQL function RPAD and LPAD.
        Parameters:
        string - the original string
        n - the target length
        padding - the padding string
        right - true if the padding should be appended at the end
        Returns:
        the padded string
      • cloneCharArray

        public static char[] cloneCharArray​(char[] chars)
        Create a new char array and copy all the data. If the size of the byte array is zero, the same array is returned.
        Parameters:
        chars - the char array (may be null)
        Returns:
        a new char array
      • trim

        public static java.lang.String trim​(java.lang.String s,
                                            boolean leading,
                                            boolean trailing,
                                            java.lang.String sp)
        Trim a character from a string.
        Parameters:
        s - the string
        leading - if leading characters should be removed
        trailing - if trailing characters should be removed
        sp - what to remove (only the first character is used) or null for a space
        Returns:
        the trimmed string
      • trimSubstring

        public static java.lang.String trimSubstring​(java.lang.String s,
                                                     int beginIndex)
        Trim a whitespace from a substring. Equivalent of substring(beginIndex).trim().
        Parameters:
        s - the string
        beginIndex - start index of substring (inclusive)
        Returns:
        trimmed substring
      • trimSubstring

        public static java.lang.String trimSubstring​(java.lang.String s,
                                                     int beginIndex,
                                                     int endIndex)
        Trim a whitespace from a substring. Equivalent of substring(beginIndex, endIndex).trim().
        Parameters:
        s - the string
        beginIndex - start index of substring (inclusive)
        endIndex - end index of substring (exclusive)
        Returns:
        trimmed substring
      • trimSubstring

        public static java.lang.StringBuilder trimSubstring​(java.lang.StringBuilder builder,
                                                            java.lang.String s,
                                                            int beginIndex,
                                                            int endIndex)
        Trim a whitespace from a substring and append it to a specified string builder. Equivalent of builder.append(substring(beginIndex, endIndex).trim()).
        Parameters:
        builder - string builder to append to
        s - the string
        beginIndex - start index of substring (inclusive)
        endIndex - end index of substring (exclusive)
        Returns:
        the specified builder
      • truncateString

        public static java.lang.String truncateString​(java.lang.String s,
                                                      int maximumLength)
        Truncates the specified string to the specified length. This method, unlike String.substring(int, int), doesn't break Unicode code points. If the specified length in characters breaks a valid pair of surrogates, the whole pair is not included into result.
        Parameters:
        s - the string to truncate
        maximumLength - the maximum length in characters
        Returns:
        the specified string if it isn't longer than the specified maximum length, and the truncated string otherwise
      • cache

        public static java.lang.String cache​(java.lang.String s)
        Get the string from the cache if possible. If the string has not been found, it is added to the cache. If there is such a string in the cache, that one is returned.
        Parameters:
        s - the original string
        Returns:
        a string with the same content, if possible from the cache
      • clearCache

        public static void clearCache()
        Clear the cache. This method is used for testing.
      • parseUInt31

        public static int parseUInt31​(java.lang.String s,
                                      int start,
                                      int end)
        Parses an unsigned 31-bit integer. Neither - nor + signs are allowed.
        Parameters:
        s - string to parse
        start - the beginning index, inclusive
        end - the ending index, exclusive
        Returns:
        the unsigned int not greater than Integer.MAX_VALUE.
      • convertHexToBytes

        public static byte[] convertHexToBytes​(java.lang.String s)
        Convert a hex encoded string to a byte array.
        Parameters:
        s - the hex encoded string
        Returns:
        the byte array
      • convertHexWithSpacesToBytes

        public static java.io.ByteArrayOutputStream convertHexWithSpacesToBytes​(java.io.ByteArrayOutputStream baos,
                                                                                java.lang.String s,
                                                                                int start,
                                                                                int end)
        Parses a hex encoded string with possible space separators and appends the decoded binary string to the specified output stream.
        Parameters:
        baos - the output stream, or null
        s - the hex encoded string
        start - the start index
        end - the end index, exclusive
        Returns:
        the specified output stream or a new output stream
      • getHexStringException

        private static DbException getHexStringException​(int code,
                                                         java.lang.String s,
                                                         int start,
                                                         int end)
      • convertBytesToHex

        public static java.lang.String convertBytesToHex​(byte[] value)
        Convert a byte array to a hex encoded string.
        Parameters:
        value - the byte array
        Returns:
        the hex encoded string
      • convertBytesToHex

        public static java.lang.String convertBytesToHex​(byte[] value,
                                                         int len)
        Convert a byte array to a hex encoded string.
        Parameters:
        value - the byte array
        len - the number of bytes to encode
        Returns:
        the hex encoded string
      • convertBytesToHex

        public static java.lang.StringBuilder convertBytesToHex​(java.lang.StringBuilder builder,
                                                                byte[] value)
        Convert a byte array to a hex encoded string and appends it to a specified string builder.
        Parameters:
        builder - string builder to append to
        value - the byte array
        Returns:
        the hex encoded string
      • convertBytesToHex

        public static java.lang.StringBuilder convertBytesToHex​(java.lang.StringBuilder builder,
                                                                byte[] value,
                                                                int len)
        Convert a byte array to a hex encoded string and appends it to a specified string builder.
        Parameters:
        builder - string builder to append to
        value - the byte array
        len - the number of bytes to encode
        Returns:
        the hex encoded string
      • appendHex

        public static java.lang.StringBuilder appendHex​(java.lang.StringBuilder builder,
                                                        long x,
                                                        int bytes)
        Appends specified number of trailing bytes from unsigned long value to a specified string builder.
        Parameters:
        builder - string builder to append to
        x - value to append
        bytes - number of bytes to append
        Returns:
        the specified string builder
      • isNumber

        public static boolean isNumber​(java.lang.String s)
        Check if this string is a decimal number.
        Parameters:
        s - the string
        Returns:
        true if it is
      • isWhitespaceOrEmpty

        public static boolean isWhitespaceOrEmpty​(java.lang.String s)
        Check if the specified string is empty or contains only whitespace.
        Parameters:
        s - the string
        Returns:
        whether the specified string is empty or contains only whitespace
      • appendTwoDigits

        public static java.lang.StringBuilder appendTwoDigits​(java.lang.StringBuilder builder,
                                                              int positiveValue)
        Append a zero-padded number from 00 to 99 to a string builder.
        Parameters:
        builder - the string builder
        positiveValue - the number to append
        Returns:
        the specified string builder
      • appendZeroPadded

        public static java.lang.StringBuilder appendZeroPadded​(java.lang.StringBuilder builder,
                                                               int length,
                                                               long positiveValue)
        Append a zero-padded number to a string builder.
        Parameters:
        builder - the string builder
        length - the number of characters to append
        positiveValue - the number to append
        Returns:
        the specified string builder
      • escapeMetaDataPattern

        public static java.lang.String escapeMetaDataPattern​(java.lang.String pattern)
        Escape table or schema patterns used for DatabaseMetaData functions.
        Parameters:
        pattern - the pattern
        Returns:
        the escaped pattern