1

我正在尝试实现一个程序,该程序可以在您键入时计算多行文本框中的单词。我可以让它计算单词,直到我按下“enter”键并输入一个单词。它不承认这一点。这是我的代码:

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 
4

9 回答 9

2
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
于 2012-02-03T01:28:24.850 回答
1

这是另一个正则表达式解决方案:

Dim WordCount = New Regex("\w+").Matches(s).Count
于 2012-02-03T01:45:28.580 回答
0

为什么不改成正则表达式,整个事情都可以这样

Dim words As MatchCollection = Regex.Matches(value, "\s+")
words.Count
于 2012-02-03T01:31:31.107 回答
0

您需要删除“返回”键

添加这些行:

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

  str = LTrim(str) 'removes blank spaces at the beginning of text
  str = RTrim(str) ' removes blank spaces at the end of text 
于 2012-02-03T01:35:05.280 回答
0

这是我数单词的方法

    Dim count As Integer = 0
    For Each word As String In Split(txtOutput.Text, " ")
        count += 1
    Next
    MsgBox("i counted " + count.ToString + " words")
于 2015-07-07T00:18:07.657 回答
0

不要使用

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

更好的

str = Replace(str, vbCrLf, " ")
于 2016-03-17T18:42:58.070 回答
0

如果您想在不使用 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 值将是 3(无需使用修剪或替换)

于 2017-10-24T09:21:59.623 回答
0
公共函数 NumWords(Txt As String) 只要
    暗淡我只要
    如果 Len(Trim(Txt)) = 0 那么
        字数 = 0
    别的
        Txt = 替换(Txt,vbNewLine,“”)
        对于 i = 255 到 2 步 -1
            Txt = 替换(Txt,空格(i),“”)
        接下来我
        NumWords = Len(Trim(Txt)) - Len(Replace(Txt, " ", "")) + 1
    万一
结束功能
于 2019-11-03T10:50:13.107 回答
0

我见过很多例子,人们计算空格并认为这些相当于单词的计数。那么这是不正确的,你不应该使用这种方法。有几种情况我可以想到这不起作用:

  1. 当空格后面没有单词时,空格数为单词数的+1。
  2. 您可以有双空格,它们也将计为 +1 字。
  3. 最重要的是,您可以让一个单词后跟一个换行符,然后再跟另一个单词。这里没有空格,所以整个事情会被错误地认为是一个词而不是两个词。

现在这就是我想出的。在现有字符串的开头添加一个空格。然后用空格字符替换所有出现的换行符。然后从字符串的开头逐个检查字符,如果当前字符是空格而后面的字符不是,那么你得到了一个单词。就那么简单!适用于连续空格、换行符和几乎任何我可以扔给它的东西。

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
于 2021-02-11T21:52:53.473 回答