user992520
user992520

Reputation:

Count words in Visual Basic

I am trying to implement a programme that counts the words in a multiline textbox as you type. I can get it counting the words until I press the "enter" key and type a word. It does not recognise this. This is my code:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  Dim str As String
  Dim i, l, words As Integer
  str = TextBox1.Text

  str = LTrim(str) 'removes blank spaces at the beginning of text
  str = RTrim(str) ' removes blank spaces at the end of text
  l = str.Length
  i = 0
  words = 0
  While (i < l)
    If str(i) = " " Then
      words = words + 1
      i = i + 1
      While str(i) = " "  ' removes more than 1  blank space
        i = i + 1
      End While
    Else
      i = i + 1
    End If

  End While

  words = words + 1 ' adds the last word

  TextBox2.Text = ("" & words)

End Sub 

Upvotes: 1

Views: 14284

Answers (9)

Sh.K
Sh.K

Reputation: 299

I have seen a lot of examples where people count empty spaces and think these are equivalent to the count of words. Well this is not correct and you should not use this method. There are several cases I can think of where this does not work:

  1. When an empty space is not followed by a word, the number of spaces is +1 the number of words.
  2. You can have double spaces, they will count as +1 word as well.
  3. Most importantly, you can have a word followed by a newline character(s) and then followed by another word. There are no spaces here, so the whole thing will be wrongfully considered one word instead of two.

Now here is what I have come up with. Add an empty space to the beginning of an existing string. Then replace all newline occurrences with empty space characters. Then check one by one the characters from the start of the string, if the current character is an empty space and character following it is not, then you got a word. Simple as that! Works with consecutive spaces, newline characters and pretty much anything I could throw at it.

Implementation in VB6:

Private Function countwords(cstring As String) As Long
cstring = " " & cstring
cstring = Replace(cstring, vbNewLine, " ")
countwords = 0
    For i = 1 To Len(cstring) - 1
        If Mid(cstring, i, 1) = " " And Mid(cstring, i + 1, 1) <> " " Then
            countwords = countwords + 1
        End If
    Next
End Function

Upvotes: 0

Junjie Fallorina
Junjie Fallorina

Reputation: 1

Public Function NumWords(Txt As String) As Long
    Dim i As Long
    If Len(Trim(Txt)) = 0 Then
        NumWords = 0
    Else
        Txt = Replace(Txt, vbNewLine, " ")
        For i = 255 To 2 Step -1
            Txt = Replace(Txt, Space(i), " ")
        Next i
        NumWords = Len(Trim(Txt)) - Len(Replace(Txt, " ", "")) + 1
    End If
End Function

Upvotes: 0

Ricardo Gonz&#225;lez
Ricardo Gonz&#225;lez

Reputation: 1423

If you would like an easy word count without using Regex

Dim s As String = " Count    the   words "

Dim iWords As Integer = 0
Dim bNonPrintable As Integer = 1

Dim len As Integer = s.Length - 1
Dim i As Integer

For i = 0 To len
    If s(i) = " "c Or s(i) = Chr(10) Or s(i) = Chr(13) Then
        If bNonPrintable = 0 Then
            iWords += 1

            bNonPrintable = i
        End If
    Else
        If bNonPrintable > 0 Then bNonPrintable = 0
    End If
Next
If bNonPrintable = 0 Then iWords += 1

iWords value will be 3 in this case (no need to use trim or replaces)

Upvotes: 0

Luc
Luc

Reputation: 1

Don't use

str = Replace(str, "chr(10)", "")  'Replaces  line feed
str = Replace(str, "chr(13)", "")  'Replaces carriage return

Better

str = Replace(str, vbCrLf, " ")

Upvotes: 0

manolo
manolo

Reputation: 1

this is my way of counting words

    Dim count As Integer = 0
    For Each word As String In Split(txtOutput.Text, " ")
        count += 1
    Next
    MsgBox("i counted " + count.ToString + " words")

Upvotes: 0

ron tornambe
ron tornambe

Reputation: 10780

Here's another regex solution:

Dim WordCount = New Regex("\w+").Matches(s).Count

Upvotes: 1

jordanhill123
jordanhill123

Reputation: 4182

You need to remove the "return" key

Add these lines:

str = Replace(str, "chr(10)", "")  'Replaces  line feed
str = Replace(str, "chr(13)", "")  'Replaces carriage return

before

  str = LTrim(str) 'removes blank spaces at the beginning of text
  str = RTrim(str) ' removes blank spaces at the end of text 

Upvotes: 0

Raymund
Raymund

Reputation: 7892

Why not change to regex that whole thing can be like this

Dim words As MatchCollection = Regex.Matches(value, "\s+")
words.Count

Upvotes: 0

GSerg
GSerg

Reputation: 78134

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Static rex As New System.Text.RegularExpressions.Regex("\b", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.Multiline)

    Label1.Text = (rex.Matches(TextBox1.Text).Count / 2).ToString()

End Sub

Upvotes: 2

Related Questions