|
-
Apr 14th, 2005, 08:02 AM
#1
Thread Starter
Hyperactive Member
Counting occurances in a string
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."
-
Apr 14th, 2005, 08:12 AM
#2
Re: Counting occurances in a string
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
-
Apr 14th, 2005, 08:12 AM
#3
Fanatic Member
Re: Counting occurances in a string
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
-
Apr 14th, 2005, 08:13 AM
#4
Addicted Member
Re: Counting occurances in a string
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
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
EDIT:
Damn, Rhino beat me to the punch 
HTH
Last edited by Luke K; Apr 14th, 2005 at 08:18 AM.
Artificial Intelligence At War! - The best game of its genre
Program your own robot and watch it fight in 3d!
Droidarena 3
If I have been useful, please Rate My Post
Support FireFox - 
Microsoft Visual Studio .NET Professional 2003
Microsoft Visual Studio 6, Enterprise Edition
Microsoft Windows XP Professional, Service Pack 2
-
Apr 14th, 2005, 08:13 AM
#5
Re: Counting occurances in a string
Something like this should work
VB Code:
Private Sub Command1_Click()
MsgBox "The word word appears in this string " & UBound(Split(Text1.Text, "word")) & " times."
End Sub
-
Apr 14th, 2005, 08:24 AM
#6
Re: Counting occurances in a string
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
-
Apr 14th, 2005, 08:28 AM
#7
Re: Counting occurances in a string
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...
-
Apr 14th, 2005, 08:30 AM
#8
Thread Starter
Hyperactive Member
Re: Counting occurances in a string
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!
-
Apr 14th, 2005, 08:52 AM
#9
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|