Recipe 5.13. Counting Words


Problem

You want to count the words in a string.

Solution

Sample code folder: Chapter 05\CountWords

Use the Split() function to split the string at each space character. The length of the resulting array is a good approximation of the number of words in the string.

Discussion

There always seems to be more than one way to get things done in Visual Basic 2005, and counting words is no exception. The following code shows one quick-and-dirty technique that requires very little coding to get the job done:

 Dim quote As String = "The important thing is not to " & _    "stop questioning. --Albert Einstein" Dim wordCount As Integer = Split(quote, Space(1)).Length MsgBox(quote & vbNewLine & "Number of words: " & _    wordCount.ToString) 

Figure 5-11 shows the resulting number of words in the string.

Figure 5-11. Splitting a string to count its words


Inaccuracies can creep in if there are multiple spaces between some words in the string, if extra spaces appear at either or both ends of the string, or if other whitespace characters (such as tabs) are involved. A little preparation of the string can help eliminate some of these problems, but at the expense of added complexity. For example, the following lines of code get rid of runs of two or more space characters, replacing them with single spaces. Adding this code just before the Split() function can provide a more accurate word count:

 Do While (quote.IndexOf(Space(2)) >= 0)    quote = quote.Replace(Space(2), Space(1)) Loop 

Similarly, you can use the Replace() method to replace all tabs with spaces (probably best done just before converting all multiple spaces to single spaces). As you can probably sense, efforts to guarantee a more accurate count cause the code to grow quickly. The best course is to decide what degree of word-counting accuracy is required, how much value to place on speed of operation, and so on before deciding how much cleanup code to add.

Another solution to this problem involves regular expressions, which are covered in Recipes 5.37, 5.38, 5.39, 5.40, 5.41 through 5.42.

See Also

Recipe 5.42 shows how to solve this same problem using a different solution.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net