Why is the length of this string longer than the number of characters in it?

后端 未结 8 1061
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 19:26

This code:

string a = \"abc\";
string b = \"A         


        
8条回答
  •  甜味超标
    2020-11-29 20:02

    This is because length() only works for Unicode code points that are no larger than U+FFFF. This set of code points is known as the Basic Multilingual Plane (BMP) and uses only 2 bytes.

    Unicode code points outside of the BMP are represented in UTF-16 using 4 byte surrogate pairs.

    To correctly count the number of characters (3), use StringInfo

    StringInfo b = new StringInfo("A

提交回复
热议问题