This question already has an answer here:
- How to get distinct characters in c# 7 answers
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:
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?
You can use LINQ:
It uses a fact, that
string
implementsIEnumerable<char>
.Without LINQ, you can do the same stuff
Distinct
does internally and useHashSet<char>
:If you handle only ANSI text in English (or characters from BMP) then 80% times if you write:
You will live happy and won't ever have any trouble. Let me post this answer only for who will really need to handle that in the proper way. I'd say everyone should but I know it's not true (quote from Wikipedia):
Problem of our first naïve solution is that it doesn't handle Unicode properly and it also doesn't consider what user perceive as character. Let's try
"
If you're using C# then Linq comes nicely to the rescue - again:
will do it.