Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding number of occurances of a string within another string 1

Status
Not open for further replies.

JohnVogel

Programmer
Apr 19, 1999
117
US
I need to figure hot to take string a, and find the number of occurances of string be within string a. I'm pretty sure this can be done with the Insr() function. I also need for it to find a string like "the dog runs" in a string such as "the big dog always runs away" as 1 instance of string A.


John Vogel
john@thecompuwizard.com
 
Say for example the string is "There is a Big yellow Dog on the front porch. The big yellow Dog likes sitting on the porch." And you want to count the occurances of "Big...Yellow...Dog...Porch", regardless of what words are in between... so in the above example the return value would be 2 (two occurances of those letters in that sequence regardless of other letters around it)... so you know what I am saying?


John Vogel
john@thecompuwizard.com
 
...mean of me not to provide an example RegExp solution
Code:
Option Explicit

Private Sub Command1_Click()
    Dim DemoText As String
    
    DemoText = "There is a Big yellow Dog on the front porch. The big yellow Dog likes sitting on the porch. Nearby was a big banana which was yellow and also another dog which was sitting under the porch"
    Debug.Print SuperMatch(DemoText, "Big Yellow Dog Porch")
End Sub


Private Function SuperMatch(strSource As String, strMatchText As String) As Long
    With CreateObject("VbScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "\b" & Join(Split(strMatchText, " "), "\b.*?\b") & "\b"
        SuperMatch = .Execute(strSource).Count
    End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top