如何计算字符串中的字符独特 [英] How to count unique characters in string

查看:115
本文介绍了如何计算字符串中的字符独特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有mystring变量=布拉布拉或myString的= 998769

Lets say we have variable myString="blabla" or mystring=998769

myString.Length; //will get you your result

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

如何获得,独特的性格算什么?我的意思是布拉布拉的结果必须是3,母鹿998769这将是4.是否存在准备去功能?有什么建议?

How to get, unique character count? I mean for "blabla" result must be 3, doe "998769" it will be 4. Is there ready to go function? any suggestions?

推荐答案

您可以使用LINQ:

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

它使用一个事实,即字符串工具的IEnumerable<焦炭方式>

没有LINQ,你可以做同样的东西鲜明确实在内部,并使用的HashSet<焦炭>

Without LINQ, you can do the same stuff Distinct does internally and use HashSet<char>:

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

这篇关于如何计算字符串中的字符独特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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