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.
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.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.