@ SuppressWarnings ( "PMD.NcssCount" ) static String escape ( String str ) { StringBuilder builder = new StringBuilder ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { char c = str . charAt ( i ) ; switch ( c ) { case ' ' : builder . append ( "\\t" ) ; break ; case ' ' : builder . append ( "\\n" ) ; break ; case ' ' : builder . append ( "\\r" ) ; break ; case ' ' : builder . append ( "\\f" ) ; break ; case ' ' : builder . append ( "\\\\" ) ; break ; case ' ' : builder . append ( "\\^" ) ; break ; case ' ' : builder . append ( "\\$" ) ; break ; case ' ' : builder . append ( "\\." ) ; break ; case ' ' : builder . append ( "\\?" ) ; break ; case ' ' : builder . append ( "\\*" ) ; break ; case ' ' : builder . append ( "\\+" ) ; break ; case ' ' : builder . append ( "\\[" ) ; break ; case ' ' : builder . append ( "\\]" ) ; break ; case ' ' : builder . append ( "\\(" ) ; break ; case ' ' : builder . append ( "\\)" ) ; break ; case ' ' : builder . append ( "\\{" ) ; break ; case ' ' : builder . append ( "\\}" ) ; break ; default : if ( c <= ' ' || c > ' ' ) { builder . append ( String . format ( "\\u%04x" , ( int ) c ) ) ; } else { builder . append ( c ) ; } break ; } } return builder . toString ( ) ; }
Escape a string so it will be interpreted as a literal character sequence when processed as a regular expression .