Java:如何从字符串“\u00C3”创建unicode ETC

发布于 2024-10-17 12:31:09 字数 192 浏览 6 评论 0原文

我有一个文件,其中的字符串手写为 \u00C3。我想创建一个由 java 中的 unicode 表示的 unicode 字符。我尝试过但找不到方法。帮助。

编辑:当我读取文本文件字符串时,将包含“\u00C3”,不是 unicode,而是 ASCII 字符 '\' 'u' '0' '0' '3'。我想从该 ASCII 字符串形成 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

锦上情书 2024-10-24 12:31:09

我在网络上的某个地方找到了这个:

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

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();
}
弱骨蛰伏 2024-10-24 12:31:09

哎呀,我有点慢了。这是我的解决方案:

package ravi;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
public class Ravi {

    private static final Pattern UCODE_PATTERN = Pattern.compile("\\\\u[0-9a-fA-F]{4}");

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("ravi.txt"));
        while (true) {
            String line = br.readLine();
            if (line == null) break;
            if (!UCODE_PATTERN.matcher(line).matches()) {
                System.err.println("Bad input: " + line);
            } else {
                String hex = line.substring(2,6);
                int number = Integer.parseInt(hex, 16);
                System.out.println(hex + " -> " + ((char) number));
            }
        }
    }

}

Dang, I was a bit slow. Here's my solution:

package ravi;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
public class Ravi {

    private static final Pattern UCODE_PATTERN = Pattern.compile("\\\\u[0-9a-fA-F]{4}");

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("ravi.txt"));
        while (true) {
            String line = br.readLine();
            if (line == null) break;
            if (!UCODE_PATTERN.matcher(line).matches()) {
                System.err.println("Bad input: " + line);
            } else {
                String hex = line.substring(2,6);
                int number = Integer.parseInt(hex, 16);
                System.out.println(hex + " -> " + ((char) number));
            }
        }
    }

}
可爱暴击 2024-10-24 12:31:09

如果您只想转义 unicode 而不想转义其他内容,可以通过编程方式创建一个函数:

private String unicodeUnescape(String string) {
   return new UnicodeUnescaper().translate(string);
}

这使用 org.apache.commons.text.translate.UnicodeUnescaper。

If you want to escape only unicode and nothing else, programmatically, you can create a function:

private String unicodeUnescape(String string) {
   return new UnicodeUnescaper().translate(string);
}

This uses org.apache.commons.text.translate.UnicodeUnescaper.

耶耶耶 2024-10-24 12:31:09

大概是这样的:

Scanner s = new Scanner( new File("myNumbers") );
while( s.hasNextLine() ) { 
   System.out.println( 
       Character.valueOf( 
           (char)(int) Integer.valueOf(
               s.nextLine().substring(2,6), 16
            )
        )
   );

Probably something along the lines:

Scanner s = new Scanner( new File("myNumbers") );
while( s.hasNextLine() ) { 
   System.out.println( 
       Character.valueOf( 
           (char)(int) Integer.valueOf(
               s.nextLine().substring(2,6), 16
            )
        )
   );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文