public static String escapeString ( String s , char escapeQuote ) { if ( ! ( escapeQuote == ' ' || escapeQuote == ' ' || escapeQuote == ' ' ) ) Kit . codeBug ( ) ; StringBuilder sb = null ; for ( int i = 0 , L = s . length ( ) ; i != L ; ++ i ) { int c = s . charAt ( i ) ; if ( ' ' <= c && c <= ' ' && c != escapeQuote && c != ' ' ) { // an ordinary print character (like C isprint()) and not " // or \ . if ( sb != null ) { sb . append ( ( char ) c ) ; } continue ; } if ( sb == null ) { sb = new StringBuilder ( L + 3 ) ; sb . append ( s ) ; sb . setLength ( i ) ; } int escape = - 1 ; switch ( c ) { case ' ' : escape = ' ' ; break ; case ' ' : escape = ' ' ; break ; case ' ' : escape = ' ' ; break ; case ' ' : escape = ' ' ; break ; case ' ' : escape = ' ' ; break ; case 0xb : escape = ' ' ; break ; // Java lacks \v. case ' ' : escape = ' ' ; break ; case ' ' : escape = ' ' ; break ; } if ( escape >= 0 ) { // an \escaped sort of character sb . append ( ' ' ) ; sb . append ( ( char ) escape ) ; } else if ( c == escapeQuote ) { sb . append ( ' ' ) ; sb . append ( escapeQuote ) ; } else { int hexSize ; if ( c < 256 ) { // 2-digit hex sb . append ( "\\x" ) ; hexSize = 2 ; } else { // Unicode. sb . append ( "\\u" ) ; hexSize = 4 ; } // append hexadecimal form of c left-padded with 0 for ( int shift = ( hexSize - 1 ) * 4 ; shift >= 0 ; shift -= 4 ) { int digit = 0xf & ( c >> shift ) ; int hc = ( digit < 10 ) ? ' ' + digit : ' ' - 10 + digit ; sb . append ( ( char ) hc ) ; } } } return ( sb == null ) ? s : sb . toString ( ) ; }
For escaping strings printed by object and array literals ; not quite the same as escape .