Top Description Fields Constructors Methods
org.apache.derby.iapi.util

public Class StringUtil

extends Object
Class Inheritance
Imports
java.util.Locale

A set of public static methods for dealing with Strings

Field Summary

Modifier and TypeField and Description
private static char[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

String where quotes have been compressed
compressQuotes
(String
string to be compressed
source
,
String
string containing two single or double quotes.
quotes
)

Compress 2 adjacent (single or double) quotes into a single (s or d) quote when found in the middle of a String.

private static String

Returns:

new version of src after substitution
doRegExpA
(String
source string in which to substitute indent
src
,
String
indentation to lead source
indent
)

Reg.exp substitute:

Pattern pat_a = Pattern.compile("\\A\\t*"); Matcher m_a = pat_a.matcher(src); src = m_a.replaceFirst(indent.toString());

private static String

Returns:

new version of src after substitution
doRegExpB
(String
source string in which to substitute
src
)

Reg.exp substitute:

Pattern pat_b = Pattern.compile("\\n+\\Z"); Matcher m_b = pat_b.matcher(formatted); formatted = m_b.replaceFirst("");

private static String

Returns:

new version of src after substitution
doRegExpC
(String
source string in which to substitute indent
src
,
String
indentation to lead source
indent
)

Reg.exp substitute:

Pattern pat_c = Pattern.compile("\\n\\t*"); Matcher m_c = pat_c.matcher(formatted); formatted = m_c.replaceAll("\n" + indent.toString());

public static String
ensureIndent(String
string to sanitize
formatted
,
int
indentation level the string is to be printed at (0,1,2..)
depth
)

Utility for formatting which bends a multi-line string into shape for outputting it in a context where there is depth tabs.

public static final String
formatForPrint(String input)

Used to print out a string for error messages, chops is off at 60 chars for historical reasons.

public static byte[]

Returns:

the converted byte array. Returns null if the length is not a multiple of 2.
fromHexString
(String
String to convert
s
,
int
starting character (zero based) to convert.
offset
,
int
number of characters to convert.
length
)

Convert a hexidecimal string generated by toHexString() back into a byte array.

public static byte[]
getAsciiBytes(String input)

Get 7-bit ASCII character array from input String.

public static String
hexDump(byte[] data)

Convert a byte array to a human-readable String for debugging purposes.

public static String
normalizeSQLIdentifier(String
syntacically correct SQL identifier
id
)

Normalize a SQL identifer, up-casing if <regular identifer>, and handling of <delimited identifer> (SQL 2003, section 5.2).

pack-priv static String

Returns:

a string quoted with the specified quote character
quoteString
(String
the string to quote
source
,
char
the character to quote the string with (' or ")
quote
)

Quote a string so that it can be used as an identifier or a string literal in SQL statements.

public static String

Returns:

the string surrounded by single quotes and with proper escaping of any single quotes inside the string
quoteStringLiteral
(String
the string to quote
string
)

Quote a string so that it can be used as a string literal in an SQL statement.

public static String
shortDBName(String canonicalDBName, char separatorChar)

Get the short database name from the canonical name.

public static String

Returns:

Sliceed value.
slice
(String
Value to slice, must be non-null.
value
,
int
Inclusive start character
beginOffset
,
int
Inclusive end character
endOffset
,
boolean
To trim or not to trim
trim
)

Return a slice (substring) of the passed in value, optionally trimmed.

public static boolean

Returns:

true if the two upppercased ENGLISH values are equal return false if s2 is null
SQLEqualsIgnoreCase
(String
first string to compare
s1
,
String
second string to compare
s2
)

Compares two strings Strings will be uppercased in english and compared equivalent to s1.equalsIgnoreCase(s2) throws NPE if s1 is null

public static String

Returns:

uppercased string
SQLToUpperCase
(String
string to uppercase
s
)

Convert string to uppercase Always use the java.util.ENGLISH locale

public static String

Returns:

the String (with hexidecimal format) form of the byte array
toHexString
(byte[]
byte array
data
,
int
starting byte (zero based) to convert.
offset
,
int
number of bytes to convert.
length
)

Convert a byte array to a String with a hexidecimal format.

public static String[]
toStringArray(Object[] objArray)

A method that receive an array of Objects and return a String array representation of that array.

public static String

Returns:

The input with trailing blanks stipped off
trimTrailing
(String str)

Trim off trailing blanks but not leading blanks

public static String

Returns:

Returns value if value is null or value.length() is less or equal to than length, otherwise a String representing value truncated to length.
truncate
(String
String to be truncated
value
,
int
Maximum length of string
length
)

Truncate a String to the given length with no warnings or error raised if it is bigger.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hex_tableback to summary
private static char[] hex_table

Constructor Detail

StringUtilback to summary
public StringUtil()

Method Detail

compressQuotesback to summary
public static String compressQuotes(String source, String quotes)

Compress 2 adjacent (single or double) quotes into a single (s or d) quote when found in the middle of a String.

Note

"""" or '''' will be compressed into "" or ''. This function assumes that the leading and trailing quote from a string or delimited identifier have already been removed.

Parameters
source:String

string to be compressed

quotes:String

string containing two single or double quotes.

Returns:String

String where quotes have been compressed

doRegExpAback to summary
private static String doRegExpA(String src, String indent)

Reg.exp substitute:

Pattern pat_a = Pattern.compile("\\A\\t*");
Matcher m_a = pat_a.matcher(src);
src = m_a.replaceFirst(indent.toString());

Parameters
src:String

source string in which to substitute indent

indent:String

indentation to lead source

Returns:String

new version of src after substitution

doRegExpBback to summary
private static String doRegExpB(String src)

Reg.exp substitute:

Pattern pat_b = Pattern.compile("\\n+\\Z");
Matcher m_b = pat_b.matcher(formatted);
formatted = m_b.replaceFirst("");

Parameters
src:String

source string in which to substitute

Returns:String

new version of src after substitution

doRegExpCback to summary
private static String doRegExpC(String src, String indent)

Reg.exp substitute:

Pattern pat_c = Pattern.compile("\\n\\t*");
Matcher m_c = pat_c.matcher(formatted);
formatted = m_c.replaceAll("\n" + indent.toString());

Parameters
src:String

source string in which to substitute indent

indent:String

indentation to lead source

Returns:String

new version of src after substitution

ensureIndentback to summary
public static String ensureIndent(String formatted, int depth)

Utility for formatting which bends a multi-line string into shape for outputting it in a context where there is depth tabs. Trailing newlines are discarded as well.

Replace "^[\t]*" with "depth" number of tabs.
Replace "\n+$" with "". Replace all "\n[\t]*" with "\n" + "depth" number of tabs.

Parameters
formatted:String

string to sanitize

depth:int

indentation level the string is to be printed at (0,1,2..)

formatForPrintback to summary
public static final String formatForPrint(String input)

Used to print out a string for error messages, chops is off at 60 chars for historical reasons.

fromHexStringback to summary
public static byte[] fromHexString(String s, int offset, int length)

Convert a hexidecimal string generated by toHexString() back into a byte array.

Parameters
s:String

String to convert

offset:int

starting character (zero based) to convert.

length:int

number of characters to convert.

Returns:byte[]

the converted byte array. Returns null if the length is not a multiple of 2.

getAsciiBytesback to summary
public static byte[] getAsciiBytes(String input)

Get 7-bit ASCII character array from input String. The lower 7 bits of each character in the input string is assumed to be the ASCII character value. Hexadecimal - Character | 00 NUL| 01 SOH| 02 STX| 03 ETX| 04 EOT| 05 ENQ| 06 ACK| 07 BEL| | 08 BS | 09 HT | 0A NL | 0B VT | 0C NP | 0D CR | 0E SO | 0F SI | | 10 DLE| 11 DC1| 12 DC2| 13 DC3| 14 DC4| 15 NAK| 16 SYN| 17 ETB| | 18 CAN| 19 EM | 1A SUB| 1B ESC| 1C FS | 1D GS | 1E RS | 1F US | | 20 SP | 21 ! | 22 " | 23 # | 24 $ | 25 % | 26 & | 27 ' | | 28 ( | 29 ) | 2A * | 2B + | 2C , | 2D - | 2E . | 2F / | | 30 0 | 31 1 | 32 2 | 33 3 | 34 4 | 35 5 | 36 6 | 37 7 | | 38 8 | 39 9 | 3A : | 3B ; | 3C < | 3D = | 3E > | 3F ? | | 40 @ | 41 A | 42 B | 43 C | 44 D | 45 E | 46 F | 47 G | | 48 H | 49 I | 4A J | 4B K | 4C L | 4D M | 4E N | 4F O | | 50 P | 51 Q | 52 R | 53 S | 54 T | 55 U | 56 V | 57 W | | 58 X | 59 Y | 5A Z | 5B [ | 5C \ | 5D ] | 5E ^ | 5F _ | | 60 ` | 61 a | 62 b | 63 c | 64 d | 65 e | 66 f | 67 g | | 68 h | 69 i | 6A j | 6B k | 6C l | 6D m | 6E n | 6F o | | 70 p | 71 q | 72 r | 73 s | 74 t | 75 u | 76 v | 77 w | | 78 x | 79 y | 7A z | 7B { | 7C | | 7D } | 7E ~ | 7F DEL|

hexDumpback to summary
public static String hexDump(byte[] data)

Convert a byte array to a human-readable String for debugging purposes.

normalizeSQLIdentifierback to summary
public static String normalizeSQLIdentifier(String id)

Normalize a SQL identifer, up-casing if <regular identifer>, and handling of <delimited identifer> (SQL 2003, section 5.2). The normal form is used internally in Derby.

Parameters
id:String

syntacically correct SQL identifier

quoteStringback to summary
pack-priv static String quoteString(String source, char quote)

Quote a string so that it can be used as an identifier or a string literal in SQL statements. Identifiers are surrounded by double quotes and string literals are surrounded by single quotes. If the string contains quote characters, they are escaped.

Parameters
source:String

the string to quote

quote:char

the character to quote the string with (' or ")

Returns:String

a string quoted with the specified quote character

See Also
quoteStringLiteral(String), IdUtil#normalToDelimited(String)
quoteStringLiteralback to summary
public static String quoteStringLiteral(String string)

Quote a string so that it can be used as a string literal in an SQL statement.

Parameters
string:String

the string to quote

Returns:String

the string surrounded by single quotes and with proper escaping of any single quotes inside the string

shortDBNameback to summary
public static String shortDBName(String canonicalDBName, char separatorChar)

Get the short database name from the canonical name.

sliceback to summary
public static String slice(String value, int beginOffset, int endOffset, boolean trim)

Return a slice (substring) of the passed in value, optionally trimmed. WARNING - endOffset is inclusive for historical reasons, unlike String.substring() which has an exclusive ending offset.

Parameters
value:String

Value to slice, must be non-null.

beginOffset:int

Inclusive start character

endOffset:int

Inclusive end character

trim:boolean

To trim or not to trim

Returns:String

Sliceed value.

SQLEqualsIgnoreCaseback to summary
public static boolean SQLEqualsIgnoreCase(String s1, String s2)

Compares two strings Strings will be uppercased in english and compared equivalent to s1.equalsIgnoreCase(s2) throws NPE if s1 is null

Parameters
s1:String

first string to compare

s2:String

second string to compare

Returns:boolean

true if the two upppercased ENGLISH values are equal return false if s2 is null

SQLToUpperCaseback to summary
public static String SQLToUpperCase(String s)

Convert string to uppercase Always use the java.util.ENGLISH locale

Parameters
s:String

string to uppercase

Returns:String

uppercased string

toHexStringback to summary
public static String toHexString(byte[] data, int offset, int length)

Convert a byte array to a String with a hexidecimal format. The String may be converted back to a byte array using fromHexString.
For each byte (b) two characaters are generated, the first character represents the high nibble (4 bits) in hexidecimal (b & 0xf0), the second character represents the low nibble (b & 0x0f).
The byte at data[offset] is represented by the first two characters in the returned String.

Parameters
data:byte[]

byte array

offset:int

starting byte (zero based) to convert.

length:int

number of bytes to convert.

Returns:String

the String (with hexidecimal format) form of the byte array

toStringArrayback to summary
public static String[] toStringArray(Object[] objArray)

A method that receive an array of Objects and return a String array representation of that array.

trimTrailingback to summary
public static String trimTrailing(String str)

Trim off trailing blanks but not leading blanks

Returns:String

The input with trailing blanks stipped off

truncateback to summary
public static String truncate(String value, int length)

Truncate a String to the given length with no warnings or error raised if it is bigger.

Parameters
value:String

String to be truncated

length:int

Maximum length of string

Returns:String

Returns value if value is null or value.length() is less or equal to than length, otherwise a String representing value truncated to length.