String (String s, String toEscape, String toUnescape, StringBuilder builder) { boolean escaped = false; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (escaped) { if (toUnescape.indexOf(ch) < 0) { builder.append('\\'); builder.append(ch); } else { if (ch=='n') builder.append('\n'); else if (ch=='r') builder.append('\r'); else if (ch=='b') builder.append('\b'); else if (ch=='t') builder.append('\t'); else if (ch=='f') builder.append('\r'); else builder.append(ch); } escaped = false; continue; } if (ch == '\\') { escaped = true; continue; } if (toEscape.indexOf(ch) >= 0) { builder.append('\\'); if (ch == '\n') ch = 'n'; else if (ch == '\b') ch = 'b'; else if (ch == '\t') ch = 't'; else if (ch == '\r') ch = 'r'; else if (ch == '\f') ch = 'f'; } builder.append(ch); } return builder.toString(); }