Java Character toChars() 方法

6 Nov 2024 | 3 分钟阅读

Character 类的 `toChars(int codePoint)` 方法通常将指定的字符转换为其 UTF-16 表示形式,该表示形式通常存储在 char 数组中。如果指定的 codePoint 是 BMP 值,则生成的 char 值数组与 codePoint 的值相同。另一方面,如果指定的 codePoint 是补充 codePoint,则生成的 char 数组包含相应的代理对。

语法

参数

codePoint:需要测试的字符的 codePoint。

返回值

`toChars(int codePoint)` 方法返回具有字符 UTF-16 表示形式的 char 数组。

示例 1

输出

The char array with the UTF-16 representation is given as: n

示例 2

输出

The char array with the UTF-16 representation is given as: o

Java Character toChars(int codePoint, char[] dst, int dstIndex) 方法

Character 类的 `toChars(int codePoint, char[] dst, int dstIndex)` 方法将指定的字符转换为其各自的 UTF-16 表示形式。

如果指定的 codePoint 是 BMP (基本多语言平面) codePoint 值,则获得的值存储在 dstIndex 中,并返回 1。另一方面,如果指定的 codePoint 是补充字符,则获得的代理值存储在 dstIndex 中,并返回 1。

语法

参数

上述方法需要三个参数

a.) codePoint,即需要测试的字符。

b.) dst 是一个字符数组,UTF-16 值存储在该数组中。

c.) dstIndex 通常是 dst 数组的起始索引,转换后的值存储在该索引处。

返回值

如果所需的 codePoint 是 BMP codePoint,则 `toChars(int codePoint, char[] dst, int dstIndex)` 方法返回 1。否则,如果所需的 codePoint 是补充 codePoint,则该方法返回 2。

示例 1

输出

The primitive is a BMP code point.
The primitive is a BMP code point.

示例 2

输出

The primitive is a BMP code point.