感谢John Kugelman的帮助。该解决方案看起来现在这个样子:
for(int codePoint : codePoints(string)) {
char[] chars = Character.toChars(codePoint);
System.out.println(codePoint + " : " + String.copyValueOf(chars));
}
随着代码点(字符串字符串) - 方法看起来像这样:
private static Iterable<Integer> codePoints(final String string) {
return new Iterable<Integer>() {
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int nextIndex = 0;
public boolean hasNext() {
return nextIndex < string.length();
}
public Integer next() {
int result = string.codePointAt(nextIndex);
nextIndex += Character.charCount(result);
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
对于UTF-16编码使用'str.getBytes( “UTF-16”) ;' – Cyrbil
您需要使用**代码点**而不是'char's。表情符号不适合16位“char”。请参阅[Java 16位字符如何支持Unicode?](http://stackoverflow.com/questions/1941613/how-does-java-16-bit-chars-support-unicode)以及[我如何遍历Unicode一个Java字符串的代码点?](http://stackoverflow.com/questions/1527856/how-can-i-iterate-through-the-unicode-codepoints-of-a-java-string)。 –
@cyrbil这有什么用? –