public static String escapeJava ( final 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 ' ' : sb . append ( "\\b" ) ; break ; case ' ' : sb . append ( "\\t" ) ; break ; case ' ' : sb . append ( "\\n" ) ; break ; case ' ' : sb . append ( "\\f" ) ; break ; case ' ' : 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 ( ' ' ) ; } sb . append ( hex ) ; } else { sb . append ( c ) ; } } } return sb . toString ( ) ; }
Escapes a string using java rules .