public static String escapeJava(String string){
  int strLen=string.length();
  StringBuilder sb=new StringBuilder(strLen);
  for (int i=0; i < strLen; i++) {
    char c=string.charAt(i);
switch (c) {
case '\b':
      sb.append("\\b");
    break;
case '\t':
  sb.append("\\t");
break;
case '\n':
sb.append("\\n");
break;
case '\f':
sb.append("\\f");
break;
case '\r':
sb.append("\\r");
break;
case '\"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
default :
if ((c < 32) || (c > 127)) {
String hex=Integer.toHexString(c);
sb.append("\\u");
for (int k=hex.length(); k < 4; k++) {
sb.append('0');
}
sb.append(hex);
}
 else {
sb.append(c);
}
}
}
return sb.toString();
}

public static String escapeJava ( String string ) { int strLen = string . length ( ) ; StringBuilder sb = new StringBuilder ( strLen ) ; for ( int i = 0 ; i < strLen ; i ++ ) { char c = string . charAt ( i ) ; switch ( c ) { case '\b' : sb . append ( "\\b" ) ; break ; case '\t' : sb . append ( "\\t" ) ; break ; case '\n' : sb . append ( "\\n" ) ; break ; case '\f' : sb . append ( "\\f" ) ; break ; case '\r' : sb . append ( "\\r" ) ; break ; case '\"' : sb . append ( "\\\"" ) ; break ; case '\\' : sb . append ( "\\\\" ) ; break ; default : if ( ( c < 32 ) || ( c > 127 ) ) { String hex = Integer . toHexString ( c ) ; sb . append ( "\\u" ) ; for ( int k = hex . length ( ) ; k < 4 ; k ++ ) { sb . append ( '0' ) ; } sb . append ( hex ) ; } else { sb . append ( c ) ; } } } return sb . toString ( ) ; }
escapes a string using java rules .
