Home
VB.NET
Word Count
This page was last reviewed on Mar 9, 2023.
Dot Net Perls
Word count. How can we count words in a String using the VB.NET language? Words are delimited by spaces and some types of punctuation.
Shows a word
The easiest way to count words is with a regular expression. We use a Regex with a pattern that matches non-word characters. The Matches() function can be used.
Example. This program imports the System.Text.RegularExpressions namespace. This allows us to use the Regex type directly in the CountWords function.
Info In CountWords, we use the Regex.Matches shared function with the pattern "\S+".
And The pattern means each match consists of one or more non-whitespace characters.
Here We use two String literals as arguments to CountWords. Then we write the word counts that were received.
Result The first sentence, from Shakespeare's play Hamlet, has ten words. The second sentence, from a nursery rhyme, has five words.
Shows a word
Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Count words in this string. Dim value As String = "To be or not to be, that is the question." Dim count1 As Integer = CountWords(value) ' Count words again. value = "Mary had a little lamb." Dim count2 As Integer = CountWords(value) ' Display counts. Console.WriteLine(count1) Console.WriteLine(count2) End Sub ''' <summary> ''' Use regular expression to count words. ''' </summary> Public Function CountWords(ByVal value As String) As Integer ' Count matches. Dim collection As MatchCollection = Regex.Matches(value, "\S+") Return collection.Count End Function End Module
10 5
Discussion. Word counting should be close to standard functionality found in programs like Microsoft Word. The function here was checked against Microsoft Word.
Result It was found to have a difference of 0.022% in word counts. This is not perfect, but it is close.
Summary. We implemented a word count function in VB.NET. By using the Count property on the MatchCollection, we can count the number of words in a way that is close to other programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 9, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.