Solved

How to find occurance of string in another string !

Posted on 2000-03-01
5
483 Views
Last Modified: 2012-05-07
Hi

I need to find the 'n'th occurance of a string in a another string. It is possible to use the 'Instr' command in VB but that returns the Integer value of the location of the first occurance of the string.However take the foll. example;

"John is a good boy. John goes to school.John is a very talkative boy.John is 12 years old"

Now this string contains the word "John"
four times. If I have to parse the above mentioned string, how do I find the 3rd occurance of the word "John" in the entire string directly.

Please advise.

Cheers,
Kapil
0
Comment
Question by:explorer007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2573752
You can use Instr in a loop like this:

Option Explicit

Private Sub Command1_Click()
    MsgBox FindOccurence("John is a good boy. John goes to school.John is a very talkative boy.John is 12 years old", _
                         "John", 3)
End Sub

Private Function FindOccurence(ByVal pstrText As String, _
                               ByVal pstrSearch As String, _
                               ByVal pintOccurence) As Integer
Dim intOccurence As Integer
Dim intPos As Integer

    intPos = 0
    Do While True
        intPos = InStr(intPos + 1, pstrText, pstrSearch)
        If intPos = 0 Then Exit Function
        intOccurence = intOccurence + 1
        If intOccurence = pintOccurence Then
            FindOccurence = intPos
            Exit Function
        End If
    Loop
End Function
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 2573784
If you use vb6

Dim str As String

str = "John is a good boy. John goes to school.John is a very talkative boy.John is 12 years old"


Dim arr

arr = Split(str, "John")
Debug.Print UBound(arr)
Debug.Print "John" & arr(3)
Erase arr


0
 
LVL 1

Accepted Solution

by:
PatrickVD earned 30 total points
ID: 2573846
Emoreau is right... There isn't a function in the native VB language that does it directly....So looping on Instr is the straighforward solution.

I might have a shorter version for getting the same result but only in 3 statements. Here's the idea :

Function InstrEx( strFind as String , Byval strSource as String, iOccurence as Integer) as Integer

     If iOccurence > 1 Then
         strSource = Replace(strSource, strFind, (iOccurence - 1) )
     End If

     InstrEx = Instr(strSource, strFind)

End Function

The idea is that you get exactly the same behaviour as the Instr function itself !

TIP: If you're only searching for the last occurence then you're better of with the InstrRev function which start the search from the end of the string !

0
 
LVL 3

Expert Comment

by:Lewy
ID: 2574047
Try:
    Dim T$, X$, I As Integer, Occurance As Integer, P As Integer
    X$ = "John"
    T$ = "John is a good boy. John goes to school.John is a very talkative boy.John is 12 years old"
    Occurance = 3
    P = 1
    For I = 1 To Occurance
       P = InStr(P + 1, T$, X$)
       If P = 0 Then Exit For
    Next I
0
 

Author Comment

by:explorer007
ID: 2603380
Thanx ...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question