Java:如何从字符串“\ u00C3”创建unicode等等 [英] Java: How to create unicode from string "\u00C3" etc

查看:183
本文介绍了Java:如何从字符串“\ u00C3”创建unicode等等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其字符串手工输入为\\\Ã。我想创建一个由java中的unicode表示的unicode字符。我试过但找不到怎么样。帮助。

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.

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

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.

推荐答案

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

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();
}

这篇关于Java:如何从字符串“\ u00C3”创建unicode等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆