如何在字符串文字中添加补充Unicode字符(例如,代码点10400)?我试过像这样放一个代理对:
String text = "TEST \uD801\uDC00";
System.out.println(text);
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.
更新:
好消息是,字符串构造正确.
UTF-8中的字节数组:54 45 53 54 20 f0 90 90 80
UTF-16中的字节数组:fe ff 0 54 0 45 0 53 0 54 0 20 d8 1 dc 0
但坏消息是,它打印不正确(在我的Fedora框中),我可以看到一个正方形而不是预期的符号(我的控制台不能正确支持unicode).
小智 15
"为我工作",究竟是什么问题?
public static void main (String[] args) throws Exception {
int cp = 0x10400;
String text = "test \uD801\uDC00";
System.out.println("cp: " + cp);
System.out.println("found: " + text.codePointAt(5));
System.out.println("len: " + text.length());
}
Run Code Online (Sandbox Code Playgroud)
输出:
cp: 66560
found: 66560
len: 7
Run Code Online (Sandbox Code Playgroud)
请注意,长度 - 与大多数String方法一样 - 处理char
s,而不是Unicode字符.非常棒的Unicode支持:)
快乐的编码.
它应该使用:
System.out.println(
"text = " + new String(Character.toChars(h))
);
Run Code Online (Sandbox Code Playgroud)
但输出是:
text = ?
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22742 次 |
最近记录: |