Java examples for java.lang:String Quote
Replace the escape sequences from the quoted string representation.
//package com.java2s; public class Main { public static void main(String[] argv) { String str = "java2s.com"; System.out.println(replaceEscapeSeq(str)); }/* ww w .j a v a 2 s .co m*/ /** Replace the escape sequences from the quoted string representation. */ public final static String replaceEscapeSeq(String str) { int i = 0; int l = str.length(); char c; String num; StringBuffer buf; int n; if ((i < l) && (str.charAt(0) == '"')) { i++; l--; } // end if buf = new StringBuffer(l + 2); while (i < l) { c = str.charAt(i++); if (c == '\\') { try { c = str.charAt(i++); switch (c) { case 'n': buf.append('\n'); break; case 't': buf.append('\t'); break; case 'b': buf.append('\b'); break; case 'r': buf.append('\r'); break; case 'f': buf.append('\f'); break; case '\\': buf.append('\\'); break; case '\'': buf.append('\''); break; case '"': buf.append('"'); break; case 'u': num = "" + str.charAt(i++) + str.charAt(i++) + str.charAt(i++) + str.charAt(i++); n = Integer.parseInt(num, 16); buf.append((char) n); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': num = "" + str.charAt(i++) + str.charAt(i++) + str.charAt(i++); n = Integer.parseInt(num, 8); buf.append((char) n); break; default: } // end switch } catch (StringIndexOutOfBoundsException e) { } catch (NumberFormatException e) { } } else { buf.append(c); } // end if } // end while return buf.toString(); } }