我在java中遇到unicodes问题。我在eclipse中编程,需要编辑" Hello World!"其次是一颗心和一只企鹅。除了企鹅,一切都很好。不知怎的,我无法编辑超过4个字符的unicode。这是我的代码:
包HelloWorld;
公共类HelloWorld扩展了MiniJava {
public static void main(String[] args) {
String s1 = "Hello World! ";
char c1 = '\u2661';
char c2 = ''; //\u1F427
write (s1+c1+c2);
}
}
你能帮帮我吗?
答案 0 :(得分:2)
正如在评论中已经提到的那样,链接到SO的另一个线程,三字节unicode字符在Java中并不容易。你必须将它转换为两个字符。 Java为您提供了一种执行此转换的方法,因此您的源代码如下所示:
public static void main(String[] args) {
String s1 = "Hello World! ";
char c1 = '\u2661';
char[] c2 = Character.toChars(0x1F427);
write (s1);
write (c1);
write (new String(c2));
}