Package org.h2.util
Class StringUtils
java.lang.Object
org.h2.util.StringUtils
A few String utility functions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char[]
private static final int[]
private static SoftReference
<String[]> private static long
private static final String[][]
private static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
addAsterisk
(String s, int index) Add an asterisk ('[*]') at the given position.static StringBuilder
appendHex
(StringBuilder builder, long x, int bytes) Appends specified number of trailing bytes from unsigned long value to a specified string builder.static StringBuilder
appendTwoDigits
(StringBuilder builder, int positiveValue) Append a zero-padded number from 00 to 99 to a string builder.static StringBuilder
appendZeroPadded
(StringBuilder builder, int length, long positiveValue) Append a zero-padded number to a string builder.static String
arrayCombine
(String[] list, char separatorChar) Combine an array of strings to one array using the given separator character.static String[]
arraySplit
(String s, char separatorChar, boolean trim) Split a string into an array of strings using the given separator.static String
Get the string from the cache if possible.static void
Clear the cache.static char[]
cloneCharArray
(char[] chars) Create a new char array and copy all the data.static String
convertBytesToHex
(byte[] value) Convert a byte array to a hex encoded string.static String
convertBytesToHex
(byte[] value, int len) Convert a byte array to a hex encoded string.static StringBuilder
convertBytesToHex
(StringBuilder builder, byte[] value) Convert a byte array to a hex encoded string and appends it to a specified string builder.static StringBuilder
convertBytesToHex
(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[]
Convert a hex encoded string to a byte array.static ByteArrayOutputStream
convertHexWithSpacesToBytes
(ByteArrayOutputStream baos, 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 String
decodeUnicodeStringSQL
(String s, int uencode) Decodes a Unicode SQL string.static String
escapeMetaDataPattern
(String pattern) Escape table or schema patterns used for DatabaseMetaData functions.private static String[]
getCache()
private static DbException
getFormatException
(String s, int i) private static DbException
getHexStringException
(int code, String s, int start, int end) static StringBuilder
indent
(StringBuilder builder, String s, int spaces, boolean newline) Indents a string with spaces and appends it to a specified builder.static boolean
Check if a String is null or empty (the length is null).static boolean
Check if this string is a decimal number.static boolean
Check if the specified string is empty or contains only whitespace.static String
javaDecode
(String s) Decode a text that is encoded as a Java string literal.static String
javaEncode
(String s) Convert a string to a Java literal using the correct escape sequences.static void
javaEncode
(String s, StringBuilder buff, boolean forSQL) Convert a string to a Java literal using the correct escape sequences.static String
Pad a string.static int
parseUInt31
(String s, int start, int end) Parses an unsigned 31-bit integer.static String
Enclose a string with double quotes.static StringBuilder
quoteIdentifier
(StringBuilder builder, String s) Enclose a string with double quotes and append it to the specified string builder.private static StringBuilder
quoteIdentifierOrLiteral
(StringBuilder builder, String s, char q) static String
quoteJavaIntArray
(int[] array) Convert an int array to the Java source code that represents this array.static String
Convert a string to the Java literal and enclose it with double quotes.static String
quoteJavaStringArray
(String[] array) Convert a string array to the Java source code that represents this array.static String
Convert a string to a SQL literal.static StringBuilder
quoteStringSQL
(StringBuilder builder, String s) Convert a string to a SQL character string literal.static String
replaceAll
(String s, String before, String after) Replace all occurrences of the before string with the after string.static String
Convert a string to lowercase using the English locale.static String
Convert a string to uppercase using the English locale.static String
Trim a character from a string.static StringBuilder
trimSubstring
(StringBuilder builder, String s, int beginIndex, int endIndex) Trim a whitespace from a substring and append it to a specified string builder.static String
trimSubstring
(String s, int beginIndex) Trim a whitespace from a substring.static String
trimSubstring
(String s, int beginIndex, int endIndex) Trim a whitespace from a substring.static String
truncateString
(String s, int maximumLength) Truncates the specified string to the specified length.static String
Decode the URL to a string.static String
Encode the string as a URL.static String
Creates an XML attribute of the form name="value".static String
Converts the data to a CDATA element.static String
xmlComment
(String data) Escapes a comment.static String
Create an XML node with optional attributes and content.static String
Create an XML node with optional attributes and content.static String
Returns <?xml version="1.0"?>static String
Escapes an XML text element.static String
Escapes an XML text element.
-
Field Details
-
softCache
-
softCacheCreatedNs
private static long softCacheCreatedNs -
HEX
private static final char[] HEX -
HEX_DECODE
private static final int[] HEX_DECODE -
TO_UPPER_CACHE_LENGTH
private static final int TO_UPPER_CACHE_LENGTH- See Also:
-
TO_UPPER_CACHE_MAX_ENTRY_LENGTH
private static final int TO_UPPER_CACHE_MAX_ENTRY_LENGTH- See Also:
-
TO_UPPER_CACHE
-
-
Constructor Details
-
StringUtils
private StringUtils()
-
-
Method Details
-
getCache
-
toUpperEnglish
Convert a string to uppercase using the English locale.- Parameters:
s
- the test to convert- Returns:
- the uppercase text
-
toLowerEnglish
Convert a string to lowercase using the English locale.- Parameters:
s
- the text to convert- Returns:
- the lowercase text
-
quoteStringSQL
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
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 tos
- the text to convert- Returns:
- the specified string builder
-
decodeUnicodeStringSQL
Decodes a Unicode SQL string.- Parameters:
s
- the string to decodeuencode
- the code point of UENCODE character, or '\\'- Returns:
- the decoded string
- Throws:
DbException
- on format exception
-
javaEncode
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
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 convertbuff
- the Java representation to returnforSQL
- true if we embedding this inside a STRINGDECODE SQL command
-
addAsterisk
Add an asterisk ('[*]') at the given position. This format is used to show where parsing failed in a statement.- Parameters:
s
- the textindex
- the position- Returns:
- the text with asterisk
-
getFormatException
-
javaDecode
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
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
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
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
Encode the string as a URL.- Parameters:
s
- the string to encode- Returns:
- the encoded string
-
urlDecode
Decode the URL to a string.- Parameters:
encoded
- the encoded URL- Returns:
- the decoded string
-
arraySplit
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 splitseparatorChar
- the separator charactertrim
- whether each element should be trimmed- Returns:
- the array list
-
arrayCombine
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 arrayseparatorChar
- the separator character- Returns:
- the combined string
-
xmlAttr
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 namevalue
- the attribute value- Returns:
- the attribute
-
xmlNode
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 nameattributes
- the attributes (may be null)content
- the content (may be null)- Returns:
- the node
-
xmlNode
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 nameattributes
- 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
Indents a string with spaces and appends it to a specified builder.- Parameters:
builder
- string builder to append tos
- the stringspaces
- the number of spacesnewline
- append a newline if there is none- Returns:
- the specified string builder
-
xmlComment
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
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
Returns <?xml version="1.0"?>- Returns:
- <?xml version="1.0"?>
-
xmlText
Escapes an XML text element.- Parameters:
text
- the text data- Returns:
- the escaped text
-
xmlText
Escapes an XML text element.- Parameters:
text
- the text dataescapeNewline
- whether to escape newlines- Returns:
- the escaped text
-
replaceAll
Replace all occurrences of the before string with the after string. UnlikeString.replaceAll(String, String)
this method readsbefore
andafter
arguments as plain strings and ifbefore
argument is an empty string this method returns original strings
.- Parameters:
s
- the stringbefore
- the old textafter
- the new text- Returns:
- the string with the before string replaced
-
quoteIdentifier
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
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 tos
- the text- Returns:
- the specified builder
-
quoteIdentifierOrLiteral
-
isNullOrEmpty
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
Pad a string. This method is used for the SQL function RPAD and LPAD.- Parameters:
string
- the original stringn
- the target lengthpadding
- the padding stringright
- 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
Trim a character from a string.- Parameters:
s
- the stringleading
- if leading characters should be removedtrailing
- if trailing characters should be removedsp
- what to remove (only the first character is used) or null for a space- Returns:
- the trimmed string
-
trimSubstring
Trim a whitespace from a substring. Equivalent ofsubstring(beginIndex).trim()
.- Parameters:
s
- the stringbeginIndex
- start index of substring (inclusive)- Returns:
- trimmed substring
-
trimSubstring
Trim a whitespace from a substring. Equivalent ofsubstring(beginIndex, endIndex).trim()
.- Parameters:
s
- the stringbeginIndex
- start index of substring (inclusive)endIndex
- end index of substring (exclusive)- Returns:
- trimmed substring
-
trimSubstring
public static StringBuilder trimSubstring(StringBuilder builder, String s, int beginIndex, int endIndex) Trim a whitespace from a substring and append it to a specified string builder. Equivalent ofbuilder.append(substring(beginIndex, endIndex).trim())
.- Parameters:
builder
- string builder to append tos
- the stringbeginIndex
- start index of substring (inclusive)endIndex
- end index of substring (exclusive)- Returns:
- the specified builder
-
truncateString
Truncates the specified string to the specified length. This method, unlikeString.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 truncatemaximumLength
- 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
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
Parses an unsigned 31-bit integer. Neither - nor + signs are allowed.- Parameters:
s
- string to parsestart
- the beginning index, inclusiveend
- the ending index, exclusive- Returns:
- the unsigned
int
not greater thanInteger.MAX_VALUE
.
-
convertHexToBytes
Convert a hex encoded string to a byte array.- Parameters:
s
- the hex encoded string- Returns:
- the byte array
-
convertHexWithSpacesToBytes
public static ByteArrayOutputStream convertHexWithSpacesToBytes(ByteArrayOutputStream baos, 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, ornull
s
- the hex encoded stringstart
- the start indexend
- the end index, exclusive- Returns:
- the specified output stream or a new output stream
-
getHexStringException
-
convertBytesToHex
Convert a byte array to a hex encoded string.- Parameters:
value
- the byte array- Returns:
- the hex encoded string
-
convertBytesToHex
Convert a byte array to a hex encoded string.- Parameters:
value
- the byte arraylen
- the number of bytes to encode- Returns:
- the hex encoded string
-
convertBytesToHex
Convert a byte array to a hex encoded string and appends it to a specified string builder.- Parameters:
builder
- string builder to append tovalue
- the byte array- Returns:
- the hex encoded string
-
convertBytesToHex
Convert a byte array to a hex encoded string and appends it to a specified string builder.- Parameters:
builder
- string builder to append tovalue
- the byte arraylen
- the number of bytes to encode- Returns:
- the hex encoded string
-
appendHex
Appends specified number of trailing bytes from unsigned long value to a specified string builder.- Parameters:
builder
- string builder to append tox
- value to appendbytes
- number of bytes to append- Returns:
- the specified string builder
-
isNumber
Check if this string is a decimal number.- Parameters:
s
- the string- Returns:
- true if it is
-
isWhitespaceOrEmpty
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
Append a zero-padded number from 00 to 99 to a string builder.- Parameters:
builder
- the string builderpositiveValue
- the number to append- Returns:
- the specified string builder
-
appendZeroPadded
Append a zero-padded number to a string builder.- Parameters:
builder
- the string builderlength
- the number of characters to appendpositiveValue
- the number to append- Returns:
- the specified string builder
-
escapeMetaDataPattern
Escape table or schema patterns used for DatabaseMetaData functions.- Parameters:
pattern
- the pattern- Returns:
- the escaped pattern
-