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

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

This code:

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


        
8条回答
  •  情歌与酒
    2020-11-29 19:58

    As the other answers have pointed out, even if there are 3 visible character they are represented with 4 char objects. Which is why the Length is 4 and not 3.

    MSDN states that

    The Length property returns the number of Char objects in this instance, not the number of Unicode characters.

    However if what you really want to know is the number of "text elements" and not the number of Char objects you can use the StringInfo class.

    var si = new StringInfo("A

提交回复
热议问题