Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Word counter in C#

This simple tutorial will show you how to create an application that counts the words in a string and also strips HTML tags if necessary.

On Sunday, May 14th 2006 at 10:04 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.7 with 6 votes)
Contextual Ads
More C# Resources
Advertisement
Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

Almost every programming language has an easy method of counting the words in a string, some use RegEx (Regular Expressions) and some use arrays to store the words of the string and then count those elements. With C# you can take both approaches and come up with a satisfying result, but in this tutorial we're going to use the latter. However, we will still use a little big of RegEx for the tag stripping feature.

For those of you who are in a hurry, here is the C# method that does the counting and the stripping of tags (HTML, XHTML, XML, etc.) from the counting. But first, make sure you add the following using reference:


using System.Text.RegularExpressions;



public static int CountWords(string strText, bool stripTags)

{

    // Declare and initialize the variable holding the number of counted words

    int countedWords = 0;

 

    // If the stripTags argument was passed as false

    if (stripTags == false)

    {

        // Simply count the words in the string by splitting them wherever a space is found

        countedWords = strText.Split(' ').Length;

    }

    else

    {

        // If the user wants to strip tags, first define the tag form

        Regex tagMatch = new Regex("<[^>]+>");

        // Replace the tags with an empty string so they are not considered in count

        strText = tagMatch.Replace(strText, "");

        // Count the words in the string by splitting them wherever a space is found

        countedWords = strText.Split(' ').Length;

    }

    // Return the number of words that were counted

    return countedWords;

}


Attached to this C# tutorial you can find a sample application that uses this method.

C# Word Counter

If you wish to learn how this simple application works, you can start a new Windows Application project in Visual Studio 2005 and add to it the minimum of a textbox where the text is being stored (txtContent), a CheckBox chkStripTags to define wether or not we want the tags stripped, a button btnCount where the counting method is called, and a textbox txtCount to show the number of words counted.

Now double click the button in Visual Studio's form designer and you should get to its Click event. Inside it add the following call to the method:


txtCount.Text = CountWords(txtContent.Text, chkStripTags.Checked).ToString();



And of course, place the method I defined earlier in the same class.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Cerber on Sunday, May 21st 2006 at 12:04 PM

Shoudn't there be somethink what count number of white space and if 2 of them are nearly then it don't count the next one, becosue without it it's useless i think.

by Gaurav Vaish on Sunday, May 28th 2006 at 12:10 AM

The RegEx should not be a simple blank but \s+.


Gaurav
http://www.edujini.in

by Rwk on Wednesday, July 26th 2006 at 03:51 AM

@ Cerber (sorry for my english)

replace strText.Split(' ')
with

strText.Split(' ',System.StringSplitOptions.RemoveEmptyEntries)
so the multiple spaces, are not considered as words

by john on Monday, April 2nd 2007 at 10:13 AM

Hi This is shockingly bad bit of code

It doesn't cater for new lines
It doesn't cater for adjacent spaces

by Joe on Thursday, May 31st 2007 at 02:45 AM

Nice solution. Very elegant with the Regular Expression. Worked great for me, thank you.

by LatexX on Monday, March 30th 2009 at 10:25 AM

Hey, thanks very very much.

Is it possible that there's a bug in C# or windows or whomever, that strips command line arguments of double quotes?

If you pass command line arguments between quotes, windows will strip the double quotes but treat the contents as one argument.

Example:

myproggy.exe this "is one command line"

will return:

args[0] = myproggy.exe
args[1] = this
args[0] = is one command line

but there will be no trace of double quotes.

Enter your your word counter. I count each arg. If there is more than one word, then double quotes have to be inserted at the beginning and end of arg.

Thanks!

by pogi on Monday, June 8th 2009 at 03:27 AM

pogi

by pogi on Monday, June 8th 2009 at 03:27 AM

pogi

by jose on Monday, June 15th 2009 at 11:03 PM

i need a phrase counter...can you try that?

by on Wednesday, October 21st 2009 at 09:12 AM


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources