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

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

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 に答える