StringUtil { public static String unescape(String escaped) { boolean escaping = false; StringBuilder newString = new StringBuilder(); for (char c : escaped.toCharArray()) { if (!escaping) { if (c == ESCAPE_CHAR) { escaping = true; } else { newString.append(c); } } else { if (c == 'n') { newString.append('\n'); } else if (c == 'r') { newString.append('\r'); } else { newString.append(c); } escaping = false; } } return newString.toString(); }  static String escape(String original); static String unescape(String escaped); final static char ESCAPE_CHAR; }
@Test public void testEmptyString() { assertEquals("", StringUtil.unescape("")); }
@Test public void testNoEscape() { assertEquals("Hello", StringUtil.unescape("Hello")); assertEquals("Hello World!", StringUtil.unescape("Hello World!")); assertEquals("Hello\tWorld!\"", StringUtil.unescape("Hello\tWorld!\"")); }
@Test public void testWithEscape() { assertEquals("Hello\\World!", StringUtil.unescape("Hello\\\\World!")); assertEquals("Hello \\\\World!", StringUtil.unescape("Hello \\\\\\\\World!")); }
@Test public void testWithLineBreaks() { assertEquals("Hello\\nWorld!", StringUtil.unescape("Hello\\\\nWorld!")); assertEquals("Hello\nWorld!", StringUtil.unescape("Hello\\nWorld!")); assertEquals("Hello\\\nWorld!", StringUtil.unescape("Hello\\\\\\nWorld!")); assertEquals("\rHello\\\nWorld!", StringUtil.unescape("\\rHello\\\\\\nWorld!")); }