protected static String _escapeQuotesAndBackslashes ( String s ) { final StringBuilder buf = new StringBuilder ( s ) ; // Walk backwards, looking for quotes or backslashes. // If we see any, insert an extra backslash into the buffer at // the same index. (By walking backwards, the index into the buffer // will remain correct as we change the buffer.) for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { char c = s . charAt ( i ) ; if ( ( c == ' ' ) || ( c == ' ' ) ) { buf . insert ( i , ' ' ) ; } // Replace any special characters with escaped versions else if ( c == ' ' ) { buf . deleteCharAt ( i ) ; buf . insert ( i , "\\n" ) ; } else if ( c == ' ' ) { buf . deleteCharAt ( i ) ; buf . insert ( i , "\\t" ) ; } else if ( c == ' ' ) { buf . deleteCharAt ( i ) ; buf . insert ( i , "\\r" ) ; } else if ( c == ' ' ) { buf . deleteCharAt ( i ) ; buf . insert ( i , "\\b" ) ; } else if ( c == ' ' ) { buf . deleteCharAt ( i ) ; buf . insert ( i , "\\f" ) ; } } return buf . toString ( ) ; }
Inserts backslashes before any occurrences of a backslash or quote in the given string . Also converts any special characters appropriately .