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
       sb.append(c);
    }
return sb.toString();
}
Advertisements

Create a free website or blog at WordPress.com.

%d bloggers like this: