Java Utililty Methods SQL Unescape

List of utility methods to do SQL Unescape

Description

The list of methods to do SQL Unescape are organized into topic(s).

Method

StringunescapeMySQLString(String s)
Unescape any MySQL escape sequences.
char chars[] = s.toCharArray();
if (chars.length < 2 || chars[0] != chars[chars.length - 1] || (chars[0] != '\'' && chars[0] != '"')) {
    throw new IllegalArgumentException("not a valid MySQL string: " + s);
int j = 1; 
int f = 0; 
for (int i = 1; i < chars.length - 1; i++) {
    if (f == 0) { 
...
StringunescapeSQL(String input)
unescape SQL
if (input.startsWith("\"") && input.endsWith("\"")) {
    input = input.substring(1, input.length() - 1);
return input.replace("\"\"", "\"").replace("\\\\", "\\");
StringunescapeSQLString(String b)
unescape SQL String
assert (b.charAt(0) == '\'');
assert (b.charAt(b.length() - 1) == '\'');
StringBuilder sb = new StringBuilder(b.length());
for (int i = 1; i + 1 < b.length(); i++) {
    if (b.charAt(i) == '\\' && i + 2 < b.length()) {
        char n = b.charAt(i + 1);
        switch (n) {
        case '0':
...
StringunescapeSQLString(String b)
unescape SQL String
Character enclosure = null;
StringBuilder sb = new StringBuilder(b.length());
for (int i = 0; i < b.length(); i++) {
    char currentChar = b.charAt(i);
    if (enclosure == null) {
        if (currentChar == '\'' || b.charAt(i) == '\"') {
            enclosure = currentChar;
        continue;
    if (enclosure.equals(currentChar)) {
        enclosure = null;
        continue;
    if (currentChar == '\\' && (i + 6 < b.length()) && b.charAt(i + 1) == 'u') {
        int code = 0;
        int base = i + 2;
        for (int j = 0; j < 4; j++) {
            int digit = Character.digit(b.charAt(j + base), 16);
            code += digit * multiplier[j];
        sb.append((char) code);
        i += 5;
        continue;
    if (currentChar == '\\' && (i + 4 < b.length())) {
        char i1 = b.charAt(i + 1);
        char i2 = b.charAt(i + 2);
        char i3 = b.charAt(i + 3);
        if ((i1 >= '0' && i1 <= '1') && (i2 >= '0' && i2 <= '7') && (i3 >= '0' && i3 <= '7')) {
            byte bVal = (byte) ((i3 - '0') + ((i2 - '0') * 8) + ((i1 - '0') * 8 * 8));
            byte[] bValArr = new byte[1];
            bValArr[0] = bVal;
            String tmp = new String(bValArr);
            sb.append(tmp);
            i += 3;
            continue;
    if (currentChar == '\\' && (i + 2 < b.length())) {
        char n = b.charAt(i + 1);
        switch (n) {
        case '0':
            sb.append("\0");
            break;
        case '\'':
            sb.append("'");
            break;
        case '"':
            sb.append("\"");
            break;
        case 'b':
            sb.append("\b");
            break;
        case 'n':
            sb.append("\n");
            break;
        case 'r':
            sb.append("\r");
            break;
        case 't':
            sb.append("\t");
            break;
        case 'Z':
            sb.append("\u001A");
            break;
        case '\\':
            sb.append("\\");
            break;
        case '%':
            sb.append("\\%");
            break;
        case '_':
            sb.append("\\_");
            break;
        default:
            sb.append(n);
        i++;
    } else {
        sb.append(currentChar);
return sb.toString();
StringunescapeSqlString(String input)
Unescape the string that comes from query.
return input;
StringunescapeSQLString(String str, String delim)
unescape SQL String
return str.replace(delim + delim, delim);