@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!")); }
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(); }
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(); } }
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(); }  }
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); }
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; }