Re: word count!!! Faster, Leaner, Meaner!
Here's a even shorter method, which is probably a little more efficient with
resources, and definitely more accurate.
1. Use a regular expression match groups of non-whitespace characters.
2. Count the number of matches.
This has the advantage of dealing with repeated spaces and the varying forms
of whitespace.
e.g.:
using System;
using System.Text.RegularExpressions;
class MainEntryPoint
{
static void Main(string [] args)
{
string Test = "Count words in this string.";
string Pattern = @"\S+";
MatchCollection Words = Regex.Matches(Test, Pattern);
Console.WriteLine("Word count is " + Words.Count);
}
}
"Russell Jones" <arj1@northstate.net> wrote:
>Here's a shorter method.
>
>1. Read the file into a string
>2. Split the resulting string using a space as a delimiter. That returns
an
>array of strings.
>3. Count the number of items in the array.