在this 帖子的上述答案中添加更多信息。
在 Java-12 中测试,应该适用于 5 以上的所有 Java 版本。
这里提到:https://stackoverflow.com/a/47505451/2987755,
无论哪个字符(其 Unicode 高于 U+FFFF)都表示为代理对,Java 将其存储为一对 char 值,即单个 Unicode 字符表示为两个相邻的 Java 字符。
正如我们在以下示例中看到的那样。
1.长度:
"?".length() //2, Expectations was it should return 1
"?".codePointCount(0,"?".length()) //1, To get the number of Unicode characters in a Java String
2。平等:
使用 Unicode \ud83c\udf09 将“?”表示为字符串,如下所示并检查相等性。
"?".equals("\ud83c\udf09") // true
Java 不支持 UTF-32
"?".equals("\u1F309") // false
3。您可以将 Unicode 字符转换为 Java 字符串
"?".equals(new String(Character.toChars(0x0001F309))) //true
4。 String.substring() 不考虑补充字符
"??".substring(0,1) //"?"
"??".substring(0,2) //"?"
"??".substring(0,4) //"??"
为了解决这个问题,我们可以使用String.offsetByCodePoints(int index, int codePointOffset)
"??".substring(0,"??".offsetByCodePoints(0,1) // "?"
"??".substring(2,"??".offsetByCodePoints(1,2)) // "?"
5。使用 BreakIterator 迭代 Unicode 字符串
6. 使用 Unicode 对字符串进行排序java.text.Collator
7.不要使用字符的toUpperCase(),toLowerCase(),方法,而是使用特定语言环境的大写和小写字符串。
8.Character.isLetter(char ch)不支持,最好使用Character.isLetter(int codePoint),对于Character类中的每个methodName(char ch)方法,都会有methodName(int codePoint)的类型,可以处理补充字符。
9.在String.getBytes()中指定charset,将Bytes转换为String,InputStreamReader,OutputStreamWriter
参考:
https://coolsymbol.com/emojis/emoji-for-copy-and-paste.html#objects
https://www.online-toolz.com/tools/text-unicode-entities-convertor.php
https://www.ibm.com/developerworks/library/j-unicode/index.html
https://www.oracle.com/technetwork/articles/javaee/supplementary-142654.html
有关示例的更多信息 image1 image2
其他值得探索的术语:Normalization、BiDi