public static String escape(String unescaped){
  StringBuilder result=new StringBuilder();
  for (  char c : unescaped.toCharArray()) {
switch (c) {
case '"':
      result.append("\\\"");
    break;
case '\\':
  result.append("\\\\");
break;
case '\n':
result.append("\\n");
break;
default :
result.append(c);
break;
}
}
return result.toString();
}

public static String escape ( String unescaped ) { StringBuilder result = new StringBuilder ( ) ; for ( char c : unescaped . toCharArray ( ) ) { switch ( c ) { case '"' : result . append ( "\\\"" ) ; break ; case '\\' : result . append ( "\\\\" ) ; break ; case '\n' : result . append ( "\\n" ) ; break ; default : result . append ( c ) ; break ; } } return result . toString ( ) ; }
esacapes quotes , newlines , and backslashes .
