public static String escape ( String str ) { int i , max ; StringBuilder result ; char c ; max = str . length ( ) ; for ( i = 0 ; i < max ; i ++ ) { if ( str . charAt ( i ) < 32 ) { break ; } } if ( i == max ) { return str ; } result = new StringBuilder ( max + 10 ) ; for ( i = 0 ; i < max ; i ++ ) { c = str . charAt ( i ) ; switch ( c ) { case ' ' : result . append ( "\\n" ) ; break ; case ' ' : result . append ( "\\r" ) ; break ; case ' ' : result . append ( "\\t" ) ; break ; case ' ' : result . append ( "\\\\" ) ; break ; default : if ( c < 32 ) { result . append ( "\\u" ) . append ( Strings . padLeft ( Integer . toHexString ( c ) , 4 , ' ' ) ) ; } else { result . append ( c ) ; } } } return result . toString ( ) ; }
escape Strings as in Java String literals