gpt4 book ai didi

c# - 计算特殊的 UTF-8 字符

转载 作者:太空狗 更新时间:2023-10-29 20:00:06 26 4
gpt4 key购买 nike

我正在寻找一种方法来计算由多个字符组成的特殊字符,但在网上找不到解决方案!

例如我想计算字符串“வாழைப்பழம”。它实际上由 6 个泰米尔字符组成,但在本例中,当我们使用正常方法查找长度时,它由 9 个字符组成。我想知道泰米尔语是唯一一种会导致此问题的编码,以及是否有解决方案。我目前正试图在 C# 中找到解决方案。

提前谢谢你=)

最佳答案

使用StringInfo.LengthInTextElements :

var text = "வாழைப்பழம";
Console.WriteLine(text.Length); // 9
Console.WriteLine(new StringInfo(text).LengthInTextElements); // 6

有关此行为的解释可以在 String.Length 的文档中找到:

The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

关于c# - 计算特殊的 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com