A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
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) |
||
|
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. 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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsShoudn'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.
The RegEx should not be a simple blank but \s+.
Gaurav
http://www.edujini.in
@ Cerber (sorry for my english)
replace strText.Split(' ')
with
strText.Split(' ',System.StringSplitOptions.RemoveEmptyEntries)
so the multiple spaces, are not considered as words
Hi This is shockingly bad bit of code
It doesn't cater for new lines
It doesn't cater for adjacent spaces
Nice solution. Very elegant with the Regular Expression. Worked great for me, thank you.
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!
pogi
pogi
i need a phrase counter...can you try that?
Related Tutorials
Related Source Code
C# Job Search