private static String escapeJavaScriptChars ( String s ) { StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char ch = s . charAt ( i ) ; switch ( ch ) { 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 ; case ' ' : sb . append ( "\\\\" ) ; break ; default : if ( ch < 32 || ch >= 127 ) { sb . append ( String . format ( "\\u%04X" , ( int ) ch ) ) ; } else { sb . append ( ch ) ; } break ; } } return sb . toString ( ) ; }
Returns a String with escaped special JavaScript characters .