Home
C#
String Count Characters
This page was last reviewed on Sep 30, 2023.
Dot Net Perls
Count characters. A special C# method can count characters in a string. Microsoft Word has an option to count characters including spaces or not including spaces.
Method info. In C# programs we can duplicate the logic from Microsoft Word. We test the solution against a word processor to ensure correctness.
Example. I tested a real-world text file with Word and developed two methods that closely match Microsoft Office's result. For the character count, we treat many spaces together as one.
Next This example iterates over its string parameter. Word 2007 counts ten spaces in a row as one—we do the same here.
Loop, String Chars
And The bool flag variable keeps track of whether the previous char was a space.
bool
Info We use the char.IsWhiteSpace method. This method handles newlines, line breaks, tabs and any whitespace.
char
Also We see a method that counts non-whitespace characters. It increments the result for each non-whitespace character.
using System; class Program { public static void Main() { const string input = "There is a cat, my friend. How are you?"; int result1 = CountChars(input); int result2 = CountNonSpaceChars(input); Console.WriteLine(result1); Console.WriteLine(result2); } /// <summary> /// Return the number of characters in a string using the same method /// as Microsoft Word 2007. Sequential spaces are not counted. /// </summary> /// <param name="value">String to count chars.</param> /// <returns>Number of chars in string.</returns> static int CountChars(string value) { int result = 0; bool lastWasSpace = false; foreach (char c in value) { if (char.IsWhiteSpace(c)) { // Only count sequential spaces one time. if (lastWasSpace == false) { result++; } lastWasSpace = true; } else { // Count other characters every time. result++; lastWasSpace = false; } } return result; } /// <summary> /// Counts the number of non-whitespace characters. /// It closely matches Microsoft Word 2007. /// </summary> /// <param name="value">String to count non-whitespaces.</param> /// <returns>Number of non-whitespace chars.</returns> static int CountNonSpaceChars(string value) { int result = 0; foreach (char c in value) { if (!char.IsWhiteSpace(c)) { result++; } } return result; } }
39 31
Tests. We discuss the accuracy of the methods when compared to Microsoft Word. I tested the two methods against Microsoft Word to make sure they are accurate.
Info They come close to Word's result, although they are off by a tiny amount.
Microsoft Word char count: 834 Method count: 830 [off by 4] Word non-whitespace count: 667 Method count: 667 [exact]
A discussion. These methods are helpful when dealing with many documents. They allow you to more accurately test the logical length of a file.
Info This will make text that uses two spaces after a period be equivalent in length to text that uses one period.
And Scanning through individual characters was fast in my research. The foreach-loop is as fast as a for-loop.
foreach
for
A summary. Here we saw methods that can count characters and non-whitespace characters. The results are similar to those of Microsoft Word.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.