加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何计算字符串中的唯一字符

发布时间:2020-12-15 19:57:45 所属栏目:百科 来源:网络整理
导读:参见英文答案 How to get distinct characters in c#????????????????????????????????????7个 假设我们有变量myString =“blabla”或mystring = 998769 myString.Length; //will get you your resultmyString.Count(char.IsLetter); //if you only want the
参见英文答案 > How to get distinct characters in c#????????????????????????????????????7个
假设我们有变量myString =“blabla”或mystring = 998769

myString.Length; //will get you your result

myString.Count(char.IsLetter);    //if you only want the count of letters:

如何获得,独特的字符数?我的意思是“blabla”结果必须是3,doe“998769”它将是4.是否准备好了功能?有什么建议?

解决方法

您可以使用LINQ:

var count = myString.Distinct().Count();

它使用一个事实,该字符串实现IEnumerable< char>.

如果没有LINQ,你可以在内部做与Distinct相同的东西并使用HashSet< char>:

var count = (new HashSet<char>(myString)).Count;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读