在 Visual Basic 中计算单词数

发布于 2025-01-02 04:20:40 字数 786 浏览 1 评论 0原文

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

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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

话少情深 2025-01-09 04:20:40
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
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
谷夏 2025-01-09 04:20:40

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

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

Here's another regex solution:

Dim WordCount = New Regex("\w+").Matches(s).Count
坚持沉默 2025-01-09 04:20:40

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

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

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

Dim words As MatchCollection = Regex.Matches(value, "\s+")
words.Count
猛虎独行 2025-01-09 04:20:40

您需要删除“返回”键

添加这些行:

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 

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 
皇甫轩 2025-01-09 04:20:40

这是我计算单词数的方法

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

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")
如日中天 2025-01-09 04:20:40

不要使用

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

更好的

str = Replace(str, vbCrLf, " ")

Don't use

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

Better

str = Replace(str, vbCrLf, " ")
假扮的天使 2025-01-09 04:20:40

如果您想在不使用正则表达式的情况下进行简单的字数统计,

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(无需使用修剪或替换)

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)

终陌 2025-01-09 04:20:40
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
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
你怎么这么可爱啊 2025-01-09 04:20:40

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

  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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文