.... or alternatively ...
Private Sub Command1_Click()
Dim sSearch As String
Dim sOrig As String
Dim pos As Long
Dim lastfound As Long
Dim total As Long
sOrig = "The man walked through the manly park, the man was happy"
sSearch = "man"
pos = InStr(1, sOrig, sSearch, vbTextCompare)
If pos > 0 Then
total = 1
lastfound = pos
Do While pos <> 0
pos = InStr(lastfound + 1, sOrig, sSearch, vbTextCompare)
If pos > 0 Then
lastfound = pos
total = total + 1
End If
Loop
End If
Print total
End Sub
--
Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Kiteman (Canada)" <NO-EMAIL REMOVED> wrote in message
news:aJNwb.495244$pl3.140416@pd7tw3no...
: Not very often that I feel qualified to answer a question here, but.....
:
: >
: > Basically, I want to do something really simple:
: >
: > Search a main string for a substring, then count how many times the
: > substring appears in the mainstring
: >
: > mainstring = "The man walked through the park, the man was
: > happy"
: >
: > substring = "man"
: >
: > Count = 2 (Because 'man' appears twice)
:
: You can use the Instr function to do this but I found Mid to be easier:
:
: --------------------------------
: mainstring = "The man walked through the park, the man was happy "
: searchstring = "man"
:
: For x = 1 To Len(mainstring)
: If UCase(searchstring) = UCase(Mid(mainstring, x, Len(searchstring)))
Then
: matches = matches + 1
: Next x
:
: lblNumberOfMatches.Caption = matches
: ---------------------------------
:
: You can take out the Ucase function if you only want a match when the case
: of all characters is the same.
: eg. "Man" <> "man"
:
: Regards,
: Tom
:
:
: