Java: How to create unicode from string "\u00C3" etc

Ravi picture Ravi · Feb 14, 2011 · Viewed 8.1k times · Source

I have a file that has strings hand typed as \u00C3. I want to create a unicode character that is being represented by that unicode in java. I tried but could not find how. Help.

Edit: When I read the text file String will contain "\u00C3" not as unicode but as ASCII chars '\' 'u' '0' '0' '3'. I would like to form unicode character from that ASCII string.

Answer

Ted Hopp picture Ted Hopp · Feb 14, 2011

I picked this up somewhere on the web:

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') {
                    // TODO: check that 4 more chars exist and are all hex digits
                    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();
}