The char data type are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value.
The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java 2 platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).
A char value, therefore, represents Basic Multilingual Plane (BMP) code points, including the surrogate code points, or code units of the UTF-16 encoding. An int value represents all Unicode code points, including supplementary code points. The lower (least significant) 21 bits of int are used to represent Unicode code points and the upper (most significant) 11 bits must be zero. Unless otherwise specified, the behavior with respect to supplementary characters and surrogate char values is as follows:
The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter('\uD840') returns false, even though this specific value if followed by any low-surrogate value in a string would represent a letter.
The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).
In the J2SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding.
# 1 楼答案
Source
# 2 楼答案
Unicode字符可以占用两个以上的字节,这通常不能保存在字符中
# 3 楼答案
除了使用
StringBuilder
,还可以使用函数 直接在Character
类中找到。功能是toChars()
并且它具有以下规范:所以你不需要确切地知道代理项对是什么样子的 喜欢,您可以直接使用代码点。示例代码 然后如下所示:
注意,代码点的数据类型是
int
,而不是char
# 4 楼答案
您不能使用单个
char
(其中包含一个UTF-16代码单元),但可以使用String
:或者:
这是一个surrogate pair(两个UTF-16代码单元,它们组合在一起形成基本多语言平面之外的单个Unicode代码点)。当然,你也需要显示你的数据来处理它