Count
charactersA special C# method can count characters in a string
. Microsoft Word has an option to count characters including spaces or not including spaces.
In C# programs we can duplicate the logic from Microsoft Word. We test the solution against a word processor to ensure correctness.
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.
string
parameter. Word 2007 counts ten spaces in a row as one—we do the same here.bool
flag variable keeps track of whether the previous char
was a space.char.IsWhiteSpace
method. This method handles newlines, line breaks, tabs and any whitespace.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
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.
Microsoft Word char count: 834 Method count: 830 [off by 4] Word non-whitespace count: 667 Method count: 667 [exact]
These methods are helpful when dealing with many documents. They allow you to more accurately test the logical length of a file.
foreach
-loop is as fast as a for
-loop.Here we saw methods that can count characters and non-whitespace characters. The results are similar to those of Microsoft Word.