List of utility methods to do String Escape
String | javaScriptUnescape(String s) Undo escaping as performed in javaScriptEscape(.) Throws an IllegalArgumentException if the string contains bad escaping. StringBuilder sb = new StringBuilder(s.length()); for (int i = 0; i < s.length();) { char c = s.charAt(i); if (c == '\\') { i = javaScriptUnescapeHelper(s, i + 1, sb); } else { sb.append(c); i++; ... |
int | javaScriptUnescapeHelper(String s, int i, StringBuilder sb) Looks for an escape code starting at index i of s, and appends it to sb. if (i >= s.length()) { throw new IllegalArgumentException( "End-of-string after escape character in [" + s + "]"); char c = s.charAt(i++); switch (c) { case 'n': sb.append('\n'); ... |
String | unescapeCString(String s) Unescape any C escape sequences (\n, \r, \\, \ooo, etc) and return the resulting string. if (s.indexOf('\\') < 0) { return s; StringBuilder sb = new StringBuilder(); int len = s.length(); for (int i = 0; i < len;) { char c = s.charAt(i++); if (c == '\\' && (i < len)) { ... |
String | unescapeMySQLString(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; ... |
String | escapeString(String value) escape String int len = value.length(); StringBuilder sb = new StringBuilder(len * 3 / 2); for (int i = 0; i < len; i++) { char c = value.charAt(i); if ((c >= ' ') && (c < 0x7f)) { if ((c == '\'') || (c == '\"') || (c == '\\')) { sb.append('\\'); sb.append(c); continue; } else if (c <= 0x7f) { switch (c) { case '\n': sb.append("\\n"); continue; case '\r': sb.append("\\r"); continue; case '\t': sb.append("\\t"); continue; sb.append("\\u"); sb.append(Character.forDigit(c >> 12, 16)); sb.append(Character.forDigit((c >> 8) & 0x0f, 16)); sb.append(Character.forDigit((c >> 4) & 0x0f, 16)); sb.append(Character.forDigit(c & 0x0f, 16)); return sb.toString(); |