InStr for second occurance of string

# Thread: InStr for second occurance of string

1. Member
Join Date
Dec 1969
Posts
51

## InStr for second occurance of string

I know how to use InStr to find the posision of the frst occurance of one string within another, but how would you go about finding the second, third, etc. (especially if the number of occurances varies)??<BR><BR>For example if I want to find all the "A"&#039;s in the word banana:<BR>If I InStr("banana", a) it returns 2. I&#039;m looking for something to return 2, 4, 6.<BR><BR>Obviously, I&#039;m pretty new at this stuff, so please type slow :)<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## Loop

i = 0<BR>Do Until i &#062; Len(theString)<BR> i = InStr(i, theString, "a")<BR>&#039;you can stick this mess into an array if you<BR>&#039;so desire<BR>Loop

3. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

## RE: InStr for second occurance of string

Use a recursive function which takes the last found Instr pos as a parameter to loop through until all the InStrs have been found, i.e. *roughly like*<BR><BR>Private Function ScanStr(str,find,LastFound)<BR>FoundAt = InStr(str,find) <BR>If FoundAt&#062;=0 then<BR> ScanStr(str,find,FoundAt)<BR>End If<BR>Response.write FoundAt <BR>End Function

4. Senior Member
Join Date
Dec 1969
Posts
5,104

## How will that ever break out?

i=0<BR>do until i &#062; len(thestring) &#039;len(thestring) = 10<BR> i = instr()<BR>loop<BR><BR>&#039;i=5<BR>do until i &#062; len(thestring) &#039;len(thestring) = 10<BR> i = instr() &#039;** that should actually be i=InStr(i + 1, theString, "a")<BR>loop<BR><BR>&#039;i=8<BR>do until i &#062; len(thestring) &#039;len(thestring) = 10<BR> i = instr() &#039;** that should actually be i=InStr(i + 1, theString, "a")<BR>loop<BR><BR>&#039;i=0<BR><BR>Wouldn&#0 39;t he need to do:<BR>i = InStr(thestring, "a")<BR>do until i = 0<BR> i = InStr(i + 1, theString, "a")<BR>loop<BR><BR>-Doug<BR>

5. Senior Member
Join Date
Dec 1969
Posts
1,913

## Ignore all of them ...

As I posted earlier to a similar question, use Split to turn it into an array and then use Ubound to get the number of occurences.<BR><BR>Have a look down the site and you will see the post.

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## Heh, yeah

And if it isn&#039;t in there, it&#039;ll loop forever..<BR><BR>Hey, give me a break... I&#039;m entitled to screwing up now and then. Besides I have IIS problems that&#039;s monopolizing my (few) brain cells.

7. Senior Member
Join Date
Dec 1969
Posts
5,104

## That depends....

The other guy only wanted to know the # of occurances.<BR><BR>This guy might want to do something with the data.<BR><BR>I stand by my loop & Instr(). Even if DG tried to throw him into an infinite loop.<BR><BR>:)

8. Senior Member
Join Date
Dec 1969
Posts
11,334

## In fact...

I just wrote what you have (sorta). Just language switching too much... basically I wrote that if (noMoreOccurences) then i = Len(theString)<BR><BR>More like while i != LengthOfString<BR><BR>Heh, close enough.

9. Senior Member
Join Date
Dec 1969
Posts
1,913

## So now you are ...

answering questions that the poster didn&#039;t ask?<BR><BR>You stand by that loop all you like, I&#039;m resting on my split so there !<BR><BR>BTW, another candidate for your bios:<BR><BR>Raven Picture can be found at:<BR>http://www.leonardointernet.com/leostaff.asp <BR><BR>He is the one calling himself Paul Hesketh and he wants to support Macs :)

10. Senior Member
Join Date
Dec 1969
Posts
11,334

## That one is the best <nm>

boo

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•