Reading unicode from plain text/file

Reading Unicode characters from a raw text and want to use as Unicode character, require to unescape the character first and then can be used as Unicode.

The code below can be used for the same.

String unescape(String s) {
    int i=0,len=s.length();
    char c;
    StringBuffer sb = new StringBuffer(len);
    while (i<len) {
        c = s.charAt(i++);
        if (c=='\\') {
            if (i<len) {
               c = s.charAt(i++);
               if (c=='u'  || c == 'U' ) {
                   c = (char) Integer.parseInt(s.substring(i,i+4),16);
                   i += 4;
               } // add other cases here as desired...
        } // fall through: \ escapes itself, quotes any character but u
return sb.toString();

Blog at

%d bloggers like this: