public static String escapeUnicode ( String s ) { int len = s . length ( ) ; int i = 0 ; while ( i < len ) { char ch = s . charAt ( i ) ; if ( ch > 255 ) { StringBuilder buf = new StringBuilder ( ) ; buf . append ( s . substring ( 0 , i ) ) ; while ( i < len ) { ch = s . charAt ( i ) ; if ( ch > 255 ) { buf . append ( "\\u" ) ; buf . append ( Character . forDigit ( ( ch >> 12 ) % 16 , 16 ) ) ; buf . append ( Character . forDigit ( ( ch >> 8 ) % 16 , 16 ) ) ; buf . append ( Character . forDigit ( ( ch >> 4 ) % 16 , 16 ) ) ; buf . append ( Character . forDigit ( ( ch ) % 16 , 16 ) ) ; } else { buf . append ( ch ) ; } i ++ ; } s = buf . toString ( ) ; } else { i ++ ; } } return s ; }
Escape all unicode characters in string .