org.h2.util
Class StringUtils

java.lang.Object
  extended by org.h2.util.StringUtils

public class StringUtils
extends java.lang.Object

A few String utility functions.


Method Summary
static java.lang.String addAsterisk(java.lang.String s, int index)
          Add an asterisk ('[*]') at the given position.
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 char[] cloneCharArray(char[] chars)
          Create a new char array and copy all the data.
static java.lang.String enclose(java.lang.String s)
          Enclose a string with '(' and ')' if this is not yet done.
static boolean equals(java.lang.String a, java.lang.String b)
          Check if two strings are equal.
static java.lang.String formatDateTime(java.util.Date date, java.lang.String format, java.lang.String locale, java.lang.String timeZone)
          Formats a date using a format string.
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 a string only contains numbers.
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 the 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 java.util.Date parseDateTime(java.lang.String date, java.lang.String format, java.lang.String locale, java.lang.String timeZone)
          Parses a date using a format string.
static java.lang.String quoteIdentifier(java.lang.String s)
          Enclose a string with double quotes.
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 quoteRemarkSQL(java.lang.String sql)
          In a string, replace block comment marks with /++ ..
static java.lang.String quoteStringSQL(java.lang.String s)
          Convert a string to a SQL 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.String unEnclose(java.lang.String s)
          Remove enclosing '(' and ')' if this text is enclosed.
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 an URL.
static java.lang.String utf8Decode(byte[] utf8)
          Convert a UTF-8 representation of a text to the text.
static byte[] utf8Encode(java.lang.String s)
          Convert the text to UTF-8 format.
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 xmlStartDoc()
          Returns
static java.lang.String xmlText(java.lang.String text)
          Escapes an XML text element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

equals

public static boolean equals(java.lang.String a,
                             java.lang.String b)
Check if two strings are equal. Here, null is equal to null.

Parameters:
a - the first value
b - the second value
Returns:
true if both are null or both are equal

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

javaEncode

public static java.lang.String javaEncode(java.lang.String s)
Convert a string to the 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

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

javaDecode

public static java.lang.String javaDecode(java.lang.String s)
                                   throws java.sql.SQLException
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
Throws:
java.sql.SQLException

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'.

Parameters:
s - the text to convert
Returns:
the Java representation

utf8Encode

public static byte[] utf8Encode(java.lang.String s)
Convert the text to UTF-8 format. For the Unicode characters 0xd800-0xdfff only one byte is returned.

Parameters:
s - the text
Returns:
the UTF-8 representation

utf8Decode

public static java.lang.String utf8Decode(byte[] utf8)
Convert a UTF-8 representation of a text to the text.

Parameters:
utf8 - the UTF-8 representation
Returns:
the text

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[]{})

enclose

public static java.lang.String enclose(java.lang.String s)
Enclose a string with '(' and ')' if this is not yet done.

Parameters:
s - the string
Returns:
the enclosed string

unEnclose

public static java.lang.String unEnclose(java.lang.String s)
Remove enclosing '(' and ')' if this text is enclosed.

Parameters:
s - the potentially enclosed string
Returns:
the string

urlEncode

public static java.lang.String urlEncode(java.lang.String s)
Encode the string as an 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

formatDateTime

public static java.lang.String formatDateTime(java.util.Date date,
                                              java.lang.String format,
                                              java.lang.String locale,
                                              java.lang.String timeZone)
                                       throws java.sql.SQLException
Formats a date using a format string.

Parameters:
date - the date to format
format - the format string
locale - the locale
timeZone - the timezone
Returns:
the formatted date
Throws:
java.sql.SQLException

parseDateTime

public static java.util.Date parseDateTime(java.lang.String date,
                                           java.lang.String format,
                                           java.lang.String locale,
                                           java.lang.String timeZone)
                                    throws java.sql.SQLException
Parses a date using a format string.

Parameters:
date - the date to parse
format - the parsing format
locale - the locale
timeZone - the timeZone
Returns:
the parsed date
Throws:
java.sql.SQLException

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

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:

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:

xmlStartDoc

public static java.lang.String xmlStartDoc()
Returns

Returns:

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

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.

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

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

quoteRemarkSQL

public static java.lang.String quoteRemarkSQL(java.lang.String sql)
In a string, replace block comment marks with /++ .. ++/.

Parameters:
sql - the string
Returns:
the resulting string

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

isNumber

public static boolean isNumber(java.lang.String s)
Check if a string only contains numbers.

Parameters:
s - the string
Returns:
true if it only contains digits 0 - 9