开发者

In ICU UnicodeString what is the difference between countChar32() and length()?

From the docs;

The length is the number of 开发者_如何学运维UChar code units are in the UnicodeString. If you want the number of code points, please use countChar32().

and

Count Unicode code points in the length UChar code units of the string.

A code point may occupy either one or two UChar code units. Counting code points involves reading all code units.

From this I am inclined to think that a code point is an actual character and a code unit is just one possible part of a character.

For example.

Say you have a unicode string like:

'foobar'

Both the length and countChar32 will be 6. Then say you have a string composed of 6 chars that take the full 32 bits to encode the length would be 12 but the countChar32 would be 6.

Is this correct?


The two values will only differ if you use characters out of the Base Multilingual Plane (BMP). These characters are represented in UTF-16 as surrogate pairs. Two 16-bit characters make up one logical character. If you use any of these, each pair counts as one 32-bit character but two elements of length.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜