且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何计算字符串中的唯一字符

更新时间:2023-02-20 07:44:24

你可以使用LINQ:

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

它使用了一个事实,即 string 实现了 IEnumerable<char>.

It uses a fact, that string implements IEnumerable<char>.

没有 LINQ,您可以在内部执行 Distinct 的相同操作并使用 HashSet:

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

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