Top Description Fields Constructors Methods
com.mysql.cj.util

public Class StringUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .UnsupportedEncodingException, java.net.URLEncoder, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, .CharsetEncoder, .StandardCharsets, .UnsupportedCharsetException, java.util.ArrayList, .Arrays, .Collections, .List, .Set, .UUID, java.util.function.BiConsumer, java.util.stream.Collectors, com.mysql.cj.Messages, .ServerVersion, com.mysql.cj.exceptions.ExceptionFactory, .WrongArgumentException

Various utility methods for converting to/from byte arrays in the platform encoding and several other String operations.

Field Summary

Modifier and TypeField and Description
pack-priv static final char[]
private static final char[]
private static final String
private static final int
private static final int
private static final int
pack-priv static final char
pack-priv static final char
pack-priv static final char

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
appendAsHex(StringBuilder builder, byte[] bytes)

public static void
appendAsHex(StringBuilder builder, int value)

public static boolean
canHandleAsServerPreparedStatementNoCache(String sql, ServerVersion serverVersion, boolean allowMultiQueries, boolean noBackslashEscapes, boolean useAnsiQuotes)

public static String

Returns:

a hex and ASCII dump
dumpAsHex
(byte[]
the data to dump as hex
byteBuffer
,
int
the number of bytes to print
length
)

Returns the given bytes as a hex and ASCII dump (up to length bytes).

private static boolean
endsWith(byte[] dataFrom, String suffix)

public static boolean

Returns:

whether searchIn ends with searchFor, ignoring case
endsWithIgnoreCase
(String
the string to search in
searchIn
,
String
the string to search for
searchFor
)

Determines whether or not the string 'searchIn' ends with the string 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a String.regionMatch(...)

public static void
public static String
escapeQuote(String str, String quotChar)

public static StringBuilder
escapeString(StringBuilder buf, String x, boolean useAnsiQuotedIdentifiers, CharsetEncoder charsetEncoder)

public static char
firstAlphaCharUc(String searchIn, int startAt)

public static char

Returns:

the first non-whitespace character, upper cased.
firstNonWsCharUc
(String
the string to search in
searchIn
)

Returns the first non-whitespace char, converted to upper case

public static char
firstNonWsCharUc(String searchIn, int startAt)

public static String

Returns:

String the string with a '+' added (if needed)
fixDecimalExponent
(String
The value as a string
dString
)

Adds '+' to decimal numbers that are positive (MySQL doesn't understand them otherwise

public static byte[]

Returns:

bytes
getBytes
(String
source string
s
,
String
java encoding
encoding
)

Returns the byte[] representation of the given string using the given encoding.

public static byte[]

Returns:

bytes
getBytes
(char[]
chars
value
)

Returns the byte[] representation of subset of the given char[] using the default/platform encoding.

public static byte[]

Returns:

bytes
getBytes
(char[]
chars
c
,
String
java encoding
encoding
)

Returns the byte[] representation of subset of the given char[] using the given encoding.

public static byte[]
getBytes(char[] value, int offset, int length)

public static byte[]

Returns:

bytes
getBytes
(char[]
chars
value
,
int
offset
offset
,
int
length
length
,
String
java encoding
encoding
)

Returns the byte[] representation of subset of the given char[] using the given encoding.

public static byte[]
public static byte[]
getBytes(String value, int offset, int length)

public static byte[]
getBytes(String value, int offset, int length, String encoding)

public static byte[]
public static byte[]

Returns:

bytes
getBytesWrapped
(String
source string
s
,
char
opening char delimiter
beginWrap
,
char
closing char delimiter
endWrap
,
String
java encoding
encoding
)

Returns the byte[] representation of the given string properly wrapped between the given char delimiters using the given encoding.

public static String

Returns:

fully qualified name
getFullyQualifiedName
(String
database name
db
,
String
identifier
entity
,
String
quote character as defined on server
quoteId
,
boolean
are we in pedantic mode
isPedantic
)

Builds and returns a fully qualified name, quoted if necessary, for the given database entity.

public static String
public static boolean

Returns:

true if src contains wildcard symbols
hasWildcards
(String
string
src
)

Does the string contain wildcard symbols ('%' or '_').

public static final void
hexEscapeBlock(byte[]
source bytes
buf
,
int
number of bytes to read
size
,
BiConsumer<Byte, Byte>
consumer for low and high bits of each byte
bc
)

Used to escape binary data with hex

public static int
indexOf(byte[] s, char c)

public static int

Returns:

the position where searchFor is found within searchIn starting from startingPosition.
indexOfIgnoreCase
(String
the string to search in
searchIn
,
String
the array of strings to search for
searchFor
)

Finds the position of a substring within a string ignoring case.

public static int

Returns:

the position where searchFor is found within searchIn starting from startingPosition.
indexOfIgnoreCase
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
the array of strings to search for
searchFor
)

Finds the position of a substring within a string ignoring case.

public static int

Returns:

the position where searchFor is found within searchIn starting from startingPosition.
indexOfIgnoreCase
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String[]
searchForSequence
searchForSequence
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of the first of a consecutive sequence of strings within a string, ignoring case, with the option to skip text delimited by given markers or within comments.

public static int

Returns:

the position where searchFor is found within searchIn starting from startingPosition.
indexOfIgnoreCase
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
the string to search for
searchFor
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of a substring within a string, ignoring case, with the option to skip text delimited by given markers or within comments.

public static int

Returns:

the position where searchFor is found within searchIn starting from startingPosition.
indexOfIgnoreCase
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
the string to search for
searchFor
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of a substring within a string, ignoring case, with the option to skip text delimited by given markers or within comments.

public static int

Returns:

the position where the next non-whitespace character is found within searchIn starting from startingPosition.
indexOfNextAlphanumericChar
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of the next alphanumeric character within a string, with the option to skip text delimited by given markers or within comments.

public static int

Returns:

the position where the next non-whitespace character is found within searchIn starting from startingPosition.
indexOfNextNonWsChar
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of the next non-whitespace character within a string, with the option to skip text delimited by given markers or within comments.

public static int

Returns:

the position where the next whitespace character is found within searchIn starting from startingPosition.
indexOfNextWsChar
(int
the position to start the search from
startingPosition
,
String
the string to search in
searchIn
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behavior of the search
searchMode
)

Finds the position of the next whitespace character within a string, with the option to skip text delimited by given markers or within comments.

public static int
indexOfQuoteDoubleAware(String searchIn, String quoteChar, int startFrom)

private static boolean
isCharAtPosNotEqualIgnoreCase(String searchIn, int pos, char firstCharOfSearchForUc, char firstCharOfSearchForLc)

protected static boolean
isCharEqualIgnoreCase(char charToCompare, char compareToCharUC, char compareToCharLC)

public static boolean
public static boolean
public static boolean

Returns:

true if the CharSequence not empty and contains only digits, false otherwise.
isStrictlyNumeric
(CharSequence
The CharSequence to check.
cs
)

Checks is the CharSequence contains digits only.

public static final boolean
isValidIdChar(char c)

public static String

Returns:

the String with all elements, joined by commas and "and".
joinWithSerialComma
(List<?>
the elements to join
elements
)

Joins all elements of the given list using serial comma (Oxford comma) rules.

public static int
lastIndexOf(byte[] s, char c)

public static boolean

Returns:

true if both strings are null or have the same value
nullSafeEqual
(String
first string to compare
str1
,
String
fecond string to compare
str2
)

Two given strings are considered equal if both are null or if they have the same string value.

public static String
padString(String stringVal, int requiredLength)

public static byte[]
quoteBytes(byte[] bytes)

public static String

Returns:

With quoteChar="`":
  • null -> null
  • abc -> `abc`
  • ab`c -> `ab``c`
  • ab"c -> `ab"c`
  • `ab``c` -> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode
With quoteChar="\"":
  • null -> null
  • abc -> "abc"
  • ab`c -> "ab`c"
  • ab"c -> "ab""c"
  • "ab""c" -> "ab""c" in non-pedantic mode or """ab""""c""" in pedantic mode
quoteIdentifier
(String
in pedantic mode (connection property pedantic=true) identifier is treated as unquoted (as it is stored in the database) even if it starts and ends with quoteChar; in non-pedantic mode if identifier starts and ends with quoteChar method treats it as already quoted and doesn't modify.
identifier
,
String
` or "
quoteChar
,
boolean
operating in pedantic mode?
isPedantic
)

Surrounds identifier with quoteChar and duplicates these symbols inside the identifier.

public static String

Returns:

  • null -> null
  • abc -> `abc`
  • ab`c -> `ab``c`
  • ab"c -> `ab"c`
  • `ab``c` -> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode
quoteIdentifier
(String
in pedantic mode (connection property pedantic=true) identifier is treated as unquoted (as it is stored in the database) even if it starts and ends with "`"; in non-pedantic mode if identifier starts and ends with "`" method treats it as already quoted and doesn't modify.
identifier
,
boolean
are we in pedantic mode
isPedantic
)

Surrounds identifier with "`" and duplicates these symbols inside the identifier.

public static boolean

Returns:

whether searchIn starts with searchFor, ignoring case
regionMatchesIgnoreCase
(String
the string to search in
searchIn
,
int
the position to start at
startAt
,
String
the string to search for
searchFor
)

Determines whether or not the string 'searchIn' contains the string 'searchFor', disregarding case and starting at 'startAt'.

public static int
safeIntParse(String intAsString)

public static String
safeTrim(String toTrim)

public static String

Returns:

the input string with all comment-delimited data removed
sanitizeProcOrFuncName
(String
the source string
src
)

Next two functions are to help DBMD check if the given string is in form of database.name and return it as "database";"name" with comments removed.

public static List<String>

Returns:

the list of strings, split by delimiter
split
(String
the string to split
stringToSplit
,
String
the string to split on
delimiter
,
boolean
should the split strings be whitespace trimmed?
trim
)

Splits stringToSplit into a list, using the given delimiter

public static List<String>

Returns:

the list of strings, split by delimiter
split
(String
the string to split
stringToSplit
,
String
the string to split on
delimiter
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
boolean
should the split strings be whitespace trimmed?
trim
)

Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.

public static List<String>

Returns:

the list of strings, split by delimiter
split
(String
the string to split
stringToSplit
,
String
the string to split on
delimiter
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
boolean
should the split strings be whitespace trimmed?
trim
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behaviour of the search
searchMode
)

Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.

public static List<String>

Returns:

the list of strings, split by delimiter
split
(String
the string to split
stringToSplit
,
String
the string to split on
delimiter
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
boolean
should the split strings be whitespace trimmed?
trim
)

Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.

public static List<String>

Returns:

the list of strings, split by delimiter
split
(String
the string to split
stringToSplit
,
String
the string to split on
delimiter
,
String
characters that delimit the beginning of a text block to skip
openingMarkers
,
String
characters that delimit the end of a text block to skip
closingMarkers
,
String
the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed, otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.
overridingMarkers
,
boolean
should the split strings be whitespace trimmed?
trim
,
Set<SearchMode>
a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the behaviour of the search
searchMode
)

Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.

public static List<String>

Returns:

an array with the database name as first element and the object name as second.
splitDBdotName
(String
the source string
source
,
String
database, if available
db
,
String
quote character as defined on server
quoteId
,
boolean
is our connection in no BackSlashEscape mode
isNoBslashEscSet
)

Splits an entity identifier into its parts (database and entity name) and returns a list containing the two elements.

private static boolean
startsWith(byte[] dataFrom, String chars)

public static boolean

Returns:

whether searchIn starts with searchFor, ignoring case
startsWithIgnoreCase
(String
the string to search in
searchIn
,
String
the string to search for
searchFor
)

Determines whether or not the string 'searchIn' starts with the string 'searchFor', dis-regarding case.

public static boolean

Returns:

true if the string starts with 'searchFor' ignoring whitespace
startsWithIgnoreCaseAndNonAlphaNumeric
(String
the string to search in
searchIn
,
String
the string to search for
searchFor
)

Determines whether or not the string 'searchIn' starts with the string 'searchFor', disregarding case,leading whitespace and non-alphanumeric characters.

public static boolean

Returns:

true if the string starts with 'searchFor' ignoring whitespace
startsWithIgnoreCaseAndWs
(String
the string to search in
searchIn
,
String
the string to search for
searchFor
)

Determines whether or not the string 'searchIn' starts with the string 'searchFor', disregarding case and leading whitespace

public static boolean

Returns:

true if the string starts with 'searchFor' ignoring whitespace
startsWithIgnoreCaseAndWs
(String
the string to search in
searchIn
,
String
the string to search for
searchFor
,
int
where to start searching
beginPos
)

Determines whether or not the string 'searchIn' contains the string 'searchFor', disregarding case and leading whitespace

public static int

Returns:

the 'searchFor' array index that matched or -1 if none matches
startsWithIgnoreCaseAndWs
(String
the string to search in
searchIn
,
String[]
the string array to search for
searchFor
)

Determines whether or not the string 'searchIn' starts with one of the strings in 'searchFor', disregarding case and leading whitespace

public static String

Returns:

a String built from the provided String array and concatenation elements.
stringArrayToString
(String[]
the String array from where to take the elements.
elems
,
String
the prefix of the resulting String.
prefix
,
String
the delimiter to be used between the N-1 elements
midDelimiter
,
String
the delimiter to be used before the last element.
lastDelimiter
,
String
the suffix of the resulting String.
suffix
)

Constructs a String containing all the elements in the String array bounded and joined by the provided concatenation elements.

public static String

Returns:

the query string with all comment-delimited data removed
stripCommentsAndHints
(final String
the query string to clean up.
source
,
final String
characters that delimit the beginning of a text block to skip
openingMarkers
,
final String
characters that delimit the end of a text block to skip
closingMarkers
,
final boolean
whether or not backslash escapes are allowed
allowBackslashEscapes
)

Removes comments and hints from the given string.

public static byte[]

Returns:

result bytes
stripEnclosure
(byte[]
bytes to strip
source
,
String
prefix
prefix
,
String
suffix
suffix
)

public static char[]

Returns:

the ASCII char array
toAsciiCharArray
(byte[]
the bytes to convert
buffer
,
int
the position to start converting
startPos
,
int
the length of the string to convert
length
)

Returns the bytes as an ASCII String.

public static String

Returns:

The ASCII String.
toAsciiString
(byte[]
the bytes representing the string
buffer
)

Returns the bytes as an ASCII String.

public static String

Returns:

the ASCII string
toAsciiString
(byte[]
the bytes to convert
buffer
,
int
the position to start converting
startPos
,
int
the length of the string to convert
length
)

Returns the bytes as an ASCII String.

public static String

Returns:

a String containing the Hex representation of the given bytes
toHexString
(byte[]
the byte array to convert
byteBuffer
,
int
the number of bytes from the given array to convert
length
)

Converts the given byte array into Hex String, stopping at given length.

public static String
toString(byte[] value, int offset, int length, String encoding)

public static String
toString(byte[] value, String encoding)

public static String
toString(byte[] value, Charset charset)

public static String
toString(byte[] value, int offset, int length)

public static String
toString(byte[] value)

public static byte[]
unquoteBytes(byte[] bytes)

public static String

Returns:

  • null -> null
  • abc -> abc
  • `abc` -> abc
  • `ab``c` -> ab`c
  • `"ab`c"` -> "ab`c"
  • `ab"c` -> ab"c
  • "abc" -> abc
  • "`ab""c`" -> `ab"c`
  • "ab`c" -> ab`c
unQuoteIdentifier
(String
identifier
identifier
,
String
` or "
quoteChar
)

Trims the identifier, removes quote chars from first and last positions and replaces double occurrences of quote char from entire identifier, i.e. converts quoted identifier into the form as it is stored in database.

public static String

Returns:

the encoded string
urlEncode
(String
the string to encode
stringToEncode
)

URL-encode the given string.

public static boolean

Returns:

true if matches
wildCompareIgnoreCase
(String
the string to search in
searchIn
,
String
the string to search for, using the 'standard' SQL wildcard chars of '%' and '_'
searchFor
)

Compares searchIn against searchForWildcard with wildcards, in a case insensitive manner.

private static int

Returns:

WILD_COMPARE_MATCH if matched, WILD_COMPARE_NO_MATCH if not matched, WILD_COMPARE_CONTINUE_WITH_WILD if not matched yet, but it may in one of following recursion rounds
wildCompareInternal
(String
the string to search in
searchIn
,
String
the string to search for, using the 'standard' SQL wildcard chars of '%' and '_'
searchFor
)

Compares searchIn against searchForWildcard with wildcards (heavily borrowed from strings/ctype-simple.c in the server sources) This method does a single passage matching for normal characters and WILDCARD_ONE (_), and recursive matching for WILDCARD_MANY (%) which may be repeated for as many anchor chars are found.

public static String
zeroFill(String value, int length)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait