What does the .NET String.Length property return? Surrogate neutral length or complete character length

后端 未结 3 1963
悲哀的现实
悲哀的现实 2020-12-14 01:56

The documentation and language varies between VS 2008 and 2010:


VS 2008 Documentation

Internally, the text is stored as a read

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 02:27

    String.Length does not account for surrogate pairs; however, the StringInfo.LengthInTextElements method does.

    StringInfo.SubstringByTextElements is similar to String.Substring, but it operates on "Text Elements", such as surrogate pairs and combining characters, as well as normal characters. The functionality of both these methods are based on the StringInfo.ParseCombiningCharacters method, which extracts the starting index of each text element and stores them in a private array.

    "The .NET Framework defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. A text element can be a base character, a surrogate pair, or a combining character sequence." - http://msdn.microsoft.com/en-us/library/system.globalization.stringinfo.aspx

提交回复
热议问题