Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do i determine the amount of characters in a string in C#?
given no specific character name????
Posted
Updated 10-Dec-15 15:11pm
v2
Comments
PIEBALDconsult 10-Dec-15 21:37pm    
You want to count the number of some character that are in the string?
Please use Improve question to add some examples of input and output.
Patrice T 10-Dec-15 22:01pm    
C# is full of tools and functionalities, but you need to learn them

 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 10-Dec-15 22:19pm    
Well, I don't know what to say...
Okay, I voted 4 despite of the pure lie of your statement. This is not entirely your fault. This is the lie from Microsoft, more exactly, unclear documentation. (The property itself is quite useful.) The question is "how to determine amount of characters". System.String.Length return not the number of characters, but something else. Surprised?
Then read Solution 2.

No need to argue that you solution is useful, is all the inquirer needs — I'm ready to agree immediately. It's just not the number of characters, and this fact is easy to check up. My code sample helps to do that easily.

—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
 
Share this answer
 
v3
Comments
Arasappan 11-Dec-15 0:02am    
:-)
George Swan 11-Dec-15 1:39am    
So are you saying that asking, how many characters are in a string is the same as asking, how long is a piece of string? Regards, George. Sorry about the frivolity, I couldn't resist it.
Sergey Alexandrovich Kryukov 11-Dec-15 11:27am    
This is as simple as this: you count the number of surrogate pairs by scanning the whole string (yes, this is O(N)).
Say, you found N pairs, which means N characters. Then the rest, Length - 2*N is the number of characters in BMP. Total number of characters is Length - 2*N + N = Length - N.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900