StringUtilities { public static String unescape(String source) { byte bytes[] = source.getBytes(UTF8); ByteArrayOutputStream result = new ByteArrayOutputStream(); for (int i=0; i<bytes.length; ++i) { if (bytes[i] != '\\') { result.write(bytes[i]); continue; } if (i + 1 == bytes.length) throw new IllegalArgumentException("Found backslash at end of input"); if (bytes[i + 1] != (byte) 'x') { switch (bytes[i + 1]) { case '\\': result.write('\\'); break; case '"': result.write('"'); break; case 't': result.write('\t'); break; case 'n': result.write('\n'); break; case 'r': result.write('\r'); break; case 'f': result.write('\f'); break; default: throw new IllegalArgumentException("Illegal escape sequence \\" + ((char) bytes[i+1]) + " found"); } ++i; continue; } if (i + 3 >= bytes.length) throw new IllegalArgumentException("Found \\x at end of input"); String hexdigits = "" + ((char) bytes[i + 2]) + ((char) bytes[i + 3]); result.write((byte) Integer.parseInt(hexdigits, 16)); i += 3; } return new String(result.toByteArray(), UTF8); }  static String escape(String source); static String escape(String source, char delimiter); static String unescape(String source); static String implode(String[] array, String sepString); static String implodeMultiline(List<String> lines); static String truncateSequencesIfNecessary(String text, int maxConsecutiveLength); static String stripSuffix(String string, String suffix); static String quote(Object object); static Set<String> split(String s);  }
@Test public void testUnescape() { assertEquals("abz019ABZ", StringUtilities.unescape("abz019ABZ")); assertEquals("\t", StringUtilities.unescape("\\t")); assertEquals("\n", StringUtilities.unescape("\\n")); assertEquals("\r", StringUtilities.unescape("\\r")); assertEquals("\"", StringUtilities.unescape("\\\"")); assertEquals("\f", StringUtilities.unescape("\\f")); assertEquals("\\", StringUtilities.unescape("\\\\")); assertEquals("" + (char) 5, StringUtilities.unescape("\\x05")); assertEquals("\tA\ncombined\r" + ((char) 5) + "5test", StringUtilities.unescape("\\tA\\ncombined\\r\\x055test")); assertEquals("A space separated string", StringUtilities.unescape("A\\x20space\\x20separated\\x20string")); }