Java:如何从字符串"\ u00C3"等创建unicode

Rav*_*avi 7 java unicode unicode-string

我有一个文件,其字符串手写为\ u00C3.我想创建一个由java中的unicode表示的unicode字符.我试过但找不到怎么样.救命.

编辑:当我读取文本文件时,字符串将包含"\ u00C3"而不是unicode但是包含ASCII字符'\''u''0''0''3'.我想从该ASCII字符串形成unicode字符.

Ted*_*opp 7

我在网上的某个地方选了这个:

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();
}
Run Code Online (Sandbox Code Playgroud)