Count words in Visual Basic

老子叫甜甜 提交于 2019-12-24 10:08:14

问题


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 

回答1:


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



回答2:


Here's another regex solution:

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



回答3:


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

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



回答4:


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 



回答5:


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")



回答6:


Don't use

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

Better

str = Replace(str, vbCrLf, " ")



回答7:


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)




回答8:


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


来源:https://stackoverflow.com/questions/9121907/count-words-in-visual-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!