如何在C#编码中确定字符串中字符的数量? [英] How Do I Determine The Amount Of Characters In A String In C# coding?

查看:114
本文介绍了如何在C#编码中确定字符串中字符的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定C#中字符串中的字符数量?

没有特定的字符名称????

How do i determine the amount of characters in a string in C#?
given no specific character name????

推荐答案

查看:

字符串.Length Property [ ^ ]

C#字符串长度属性 [< a href =http://www.dotnetperls.com/string-lengthtarget =_ blanktitle =新窗口> ^ ]
Check out:
String.Length Property[^]
C# String Length Property[^]


也很抱歉对一个非常简单的问题的高级答案,但是,为了完整起见,重要的是要提及。请查看我过去对其他问题的回答:如何检索char的unicode表示? [ ^ ]。



除其他外,我解释一下,不幸的是, String.Length 并没有真正返回字符数。惊喜?它曾经让我感到惊讶。实际上,它返回16位代码字的数量。每个这样的单词仅代表具有BMP(代码点0xffff或更小)的字符的单个字符。另请参阅: https://en.wikipedia.org/wiki/Plane_%28Unicode%29#Basic_Multilingual_Plane [ ^ ]。



如果您的字符串的字符数大于MBP,则每个此类字符都由代理项对表示。我不想重复这一切;请阅读我过去的答案,了解详细信息和链接。此解决方案还应解释如何查找真实字符数。它还解释了代码点和角色的真正含义。



-SA
Sorry for too advanced answer to a very simple question, but, for completeness, it's important to mention. Please see my past answer to a different question: how to retrieve the unicode representation for a char?[^].

Among other things, I explain that, unfortunately, String.Length does not really return the number of characters. Surprise? It used to be surprise for me, too. In fact, it returns the number of 16-bit code words. Each such word represent a single character only for a character withing BMP (code point 0xffff or less). See also: https://en.wikipedia.org/wiki/Plane_%28Unicode%29#Basic_Multilingual_Plane[^].

If your string has characters above MBP, each such character is represented by a surrogate pair. I don't want to repeat all that; please read my past answer for detailed information and links. This solution should also explain how to find the "real" number of characters. It also explains what a code point and a character really is.

—SA


这篇关于如何在C#编码中确定字符串中字符的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆