Vb6 我想知道VB0.6中文本框中一个单词的发生时间
想知道Visual BASIC v.0.6中文本框中一个单词的出现次数吗? 我试着使用列表中的计数器,但效果不好 例如:在下面的句子中: “去玩,回家”。。。动词“go”出现两次..然后。。我想要计算动词“go”出现次数的代码,并通过标签对我说:例如:2次 例如:在下面的句子中:Vb6 我想知道VB0.6中文本框中一个单词的发生时间,vb6,Vb6,想知道Visual BASIC v.0.6中文本框中一个单词的出现次数吗? 我试着使用列表中的计数器,但效果不好 例如:在下面的句子中: “去玩,回家”。。。动词“go”出现两次..然后。。我想要计算动词“go”出现次数的代码,并通过标签对我说:例如:2次 例如:在下面的句子中: “去玩,回家”。。。动词“go”出现两次..然后。。我想要计算动词“go”出现次数的代码,并通过标签对我说:例如:2次尝试以下代码:- Dim txt$, find$, i1%, count% txt = "go t
“去玩,回家”。。。动词“go”出现两次..然后。。我想要计算动词“go”出现次数的代码,并通过标签对我说:例如:2次尝试以下代码:-
Dim txt$, find$, i1%, count%
txt = "go to play and go to home"
find = "go"
i1 = 0
Do
i1 = InStr(i1 + 1, txt, find)
If i1 > 0 Then
count = count + 1
i1 = i1 + Len(find)
Else
Exit Do
End If
Loop
MsgBox count
请尝试以下代码:-
Dim txt$, find$, i1%, count%
txt = "go to play and go to home"
find = "go"
i1 = 0
Do
i1 = InStr(i1 + 1, txt, find)
If i1 > 0 Then
count = count + 1
i1 = i1 + Len(find)
Else
Exit Do
End If
Loop
MsgBox count
您可以使用replace()从字符串中删除该单词,然后将结果字符串的长度与原始字符串的长度进行比较,并根据单词的长度进行区分
Option Explicit
Private Sub Command1_Click()
Label1.Caption = CStr(CountWord(Text1.Text, "go")) & " times"
End Sub
Private Sub Form_Load()
Text1.Text = " go to play and go to home"
End Sub
Private Function CountWord(strText As String, strWord As String) As Long
CountWord = (Len(strText) - Len(Replace(strText, strWord, ""))) / Len(strWord)
End Function
上面的CountWord函数在strText中查找strWord的数量,它不搜索单独的单词,但如果strWord是较大单词的一部分,它还会在计数中添加一个
例如,“go to play and go to home to search on google”将计算“go”的3个实例。您可以使用replace()从字符串中删除该单词,然后将结果字符串的长度与原始字符串的长度进行比较,并根据单词的长度进行区分
Option Explicit
Private Sub Command1_Click()
Label1.Caption = CStr(CountWord(Text1.Text, "go")) & " times"
End Sub
Private Sub Form_Load()
Text1.Text = " go to play and go to home"
End Sub
Private Function CountWord(strText As String, strWord As String) As Long
CountWord = (Len(strText) - Len(Replace(strText, strWord, ""))) / Len(strWord)
End Function
上面的CountWord函数在strText中查找strWord的数量,它不搜索单独的单词,但如果strWord是较大单词的一部分,它还会在计数中添加一个
例如,“go to play and go to home to search on google”将计算“go”的3个实例。您可以使用正则表达式计算单词出现次数
Private Sub Form_Load()
Debug.Print CountWords(" go to play and G.O to home to search on g.o" & vbCrLf & "ogle", "g.o")
End Sub
Private Function CountWords(sText As String, sWord As String) As Long
Dim sPattern As String
sPattern = pvInitRegExp("[-[\]{}()*+?.,\\^$|#\s]").Replace(sWord, "\$&")
CountWords = pvInitRegExp("\s" & sPattern & "\s").Execute(sText).Count
End Function
Private Function pvInitRegExp(sPattern As String) As Object
Set pvInitRegExp = CreateObject("VBScript.RegExp")
With pvInitRegExp
.Pattern = sPattern
.Global = True
.IgnoreCase = True
.MultiLine = True
End With
End Function
这也考虑了单词的边界,所以“google”不被计算。您可以使用正则表达式来计算单词的出现次数
Private Sub Form_Load()
Debug.Print CountWords(" go to play and G.O to home to search on g.o" & vbCrLf & "ogle", "g.o")
End Sub
Private Function CountWords(sText As String, sWord As String) As Long
Dim sPattern As String
sPattern = pvInitRegExp("[-[\]{}()*+?.,\\^$|#\s]").Replace(sWord, "\$&")
CountWords = pvInitRegExp("\s" & sPattern & "\s").Execute(sText).Count
End Function
Private Function pvInitRegExp(sPattern As String) As Object
Set pvInitRegExp = CreateObject("VBScript.RegExp")
With pvInitRegExp
.Pattern = sPattern
.Global = True
.IgnoreCase = True
.MultiLine = True
End With
End Function
这也考虑了词的边界,所以“谷歌”不算在内