I have a string that contains text. How do I count the number of occurances of one string in the other.
e.g. How do I count how many times "word" is in "These are my word examples. My word on it."
Printable View
I have a string that contains text. How do I count the number of occurances of one string in the other.
e.g. How do I count how many times "word" is in "These are my word examples. My word on it."
Something as simple as the following sample may work for you:
VB Code:
Private Sub Command1_Click() Dim iTotal%, iPos% Dim strText$ strText = "These are my word examples. My word on it." iPos = 1 Do While iPos <= Len(strText) If InStr(iPos, UCase(strText), "WORD") > 0 Then iTotal = iTotal + 1 iPos = InStr(iPos, UCase(strText), "WORD") + Len("WORD") Else Exit Do End If Loop MsgBox "Total occurences - " & iTotal End Sub
Hi, one way could be to use regular expressions.
Add a reference to Microsoft VBScript Regular Expressions 5.5 to you project and try this:
VB Code:
Dim objRegExp As VBScript_RegExp_55.RegExp Set objRegExp = New VBScript_RegExp_55.RegExp With objRegExp .Pattern = "word" .IgnoreCase = True .Global = True MsgBox .Execute("These are my word examples. My word on it.").Count End With
HTH
There are two ways off the top of my head I can think of performing this task accurately. Here they are:
VB Code:
Private Sub Form_Load() Dim stgSearch As String Dim stgWordCount() As String stgSearch = "I wrote these words, I put my word on it!" stgWordCount = Split(stgSearch, "word", -1) Call MsgBox(UBound(stgWordCount)) End Sub
OR
EDIT:VB Code:
Private Sub Form_Load() Dim stgSearch As String Dim intCurr As Integer Dim intOccur As Integer Dim intLen As Integer stgSearch = "I wrote these words, I put my word on it!" intOccur = InStr(1, stgSearch, "word") If intOccur = 0 Then Call MsgBox("There are no occurances of " & Chr(34) & "word" & Chr(34) & _ "in that sentence") Else intLen = 1 While InStr(intLen, stgSearch, "word") > 0 intCurr = InStr(intLen, stgSearch, "word") intTotal = intTotal + 1 intLen = intLen + intCurr + 1 Wend Call MsgBox("There were " & intTotal & " occurances of " & Chr(34) & _ "word" & Chr(34) & " in that sentence") End If Unload Me End Sub
Damn, Rhino beat me to the punch :lol:
HTH :cool:
Something like this should workVB Code:
Private Sub Command1_Click() MsgBox "The word word appears in this string " & UBound(Split(Text1.Text, "word")) & " times." End Sub
Yet another way...
VB Code:
Private Sub Form_Load() Dim strtext As String, strSearch As String strtext = UCase("These are my word examples. My word on it.") strSearch = UCase("word") Debug.Print "Times WORD in string = "; (Len(strtext) - Len(Replace(strtext, strSearch, ""))) / Len(strSearch) End Sub
Actually the method I posted, and most of the others, will find the "word" imbedded in other words and count those also...
I've made some posts recently about parsing a sentence - looking for spaces and commas and other punctuation.
If you are looking for perfect accuracy, then you need to look at each word one word at a time - that's going to require a loop through the string looking at each byte one at a time and checking for puncuation characters...
What fabulous examples. Thank you! :)
Let me explain what I am trying to do. I have a textbox called Keywords. I have another textbox called ArticleText.
The idea is that I enter words into the Keywords field. e.g. "All about me"
Then you write the article. I have code that is triggered - using the Change event - that does an on-the-fly word count. But I also need an on the fly count of the Keywords.
e.g. ArticleText="This article is about me and my friends"
This would give a keyword count of 2 since the words about occured once and me occured once.
So, I figured I would split the Keywords into an array.
'Put Keywords into array
Dim list As Variant
Dim strSentence As String
Dim counter As Integer
strSentence = Keywords.Text
strSentence = Trim(strSentence)
list = Split(strSentence, " ")
Now I need to do some kind of loop using the Change event so that it does a Keyword count.
Any ideas?
Hope this makes sense. I'm new to Visual Basic 6 but am amazed at how quickly you can developed stuff. It's my new hobby! :bigyello:
If you are going to do an on the fly count a loop wouldnt be advisable, because the more the user enters the longer the loop will have to go for and it will keep building up and up.
I suggest using one of the many Split methods that everyone has posted, using your keyword string as the delimiter parameter.
The attatched screenshot is a working example
HTH :cool: