-
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks