相关疑难解决方法(0)

Java charAt与具有两个代码单元的字符一起使用

来自Core Java,第一卷.第1版,第9版,p.69:

字符ℤ需要两个UTF-16编码的代码单元.调用

String sentence = "? is the set of integers"; // for clarity; not in book
char ch = sentence.charAt(1)
Run Code Online (Sandbox Code Playgroud)

不返回空格而是返回second的第二个代码单元.

但它似乎sentence.charAt(1) 确实返回了一个空间.例如,if以下代码中的语句求值为true.

String sentence = "? is the set of integers";
if (sentence.charAt(1) == ' ')
    System.out.println("sentence.charAt(1) returns a space");
Run Code Online (Sandbox Code Playgroud)

为什么?

我在Ubuntu 12.10上使用JDK SE 1.7.0_09,如果它是相关的.

java unicode utf-16 surrogate-pairs astral-plane

13
推荐指数
2
解决办法
7313
查看次数

标签 统计

astral-plane ×1

java ×1

surrogate-pairs ×1

unicode ×1

utf-16 ×1