在Visual Basic中计数字 [英] Count words in Visual Basic
本文介绍了在Visual Basic中计数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图实现一个程序,在您输入时计算多行文本框中的单词。我可以得到它计数的字,直到我按输入键和键入一个单词。它不认识这一点。这是我的代码:
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)'删除文本开头的空格
str = RTrim(str)'删除文本末尾的空格
l = str.Length
i = 0
words = 0
While(i如果str(i)=那么
字=字+ 1
i = i + 1
删除多于1个空格
i = i + 1
结束时
否则
i = i + 1
结束如果
结束while
words = words + 1'添加最后一个字
TextBox2.Text =(& words)
End Sub
解决方案
Private Sub TextBox1_TextChanged ByVal sender作为System.Object,ByVal e As System.EventArgs)处理TextBox1.TextChanged
静态rex作为新的System.Text.RegularExpressions.Regex(\b,System.Text.RegularExpressions。 RegexOptions.Compiled或System.Text.RegularExpressions.RegexOptions.Multiline)
Label1.Text =(rex.Matches(TextBox1.Text).Count / 2).ToString()
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
解决方案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
这篇关于在Visual Basic中计数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文