Home All Groups Group Topic Archive Search About

No Recursive instr?!!

Author
26 Sep 2006 1:03 PM
Anil Gupte
I must have missed it.  It cannot be possible.  Or may be I am living in the
past of my lisp programming days.

There must be a function to find the nth occurrence of a string within
another.  instr only gives you the first one.  Of course I can write a
function that recurses n times to get the nth occurrence, but there must be
a native version, right?  To clarify, lets say I want the 3rd x in this
string:

strMine="xxoooxoxoxo"

instrNth=(strMine, "x", 3)
returning 6

Any ideas?

Author
26 Sep 2006 2:10 PM
Maate
Depending on your case, I guess you can profit from regex? e.g.:

dim re as regex = new regex(pattern, regexoptions.multiline) ' e.g.
pattern = "x"
dim myMatches as matchcollection = re.matches(source-string) 'e.g.
source-string = "xxooxoxoxo"

myMatches(n) = n'th occurrence of some match 'e.g. myMatches(2) = x
myMatches(n).index = the index of the n'th occ. 'e.g.
myMatches(2).index = 4

anyway - it's more pretty than writing a recursive instr function, I
guess...



Anil Gupte skrev:

Show quoteHide quote
> I must have missed it.  It cannot be possible.  Or may be I am living in the
> past of my lisp programming days.
>
> There must be a function to find the nth occurrence of a string within
> another.  instr only gives you the first one.  Of course I can write a
> function that recurses n times to get the nth occurrence, but there must be
> a native version, right?  To clarify, lets say I want the 3rd x in this
> string:
>
> strMine="xxoooxoxoxo"
>
> instrNth=(strMine, "x", 3)
> returning 6
>
> Any ideas?
> --
> Anil Gupte
> www.keeninc.net
> www.icinema.com
Author
27 Sep 2006 5:34 AM
Anil Gupte
Wow!  Regex is quite comprehensive - I will have to sit down and study it
for a while, before I can use it.

Thanx!
Show quoteHide quote
"Maate" <mor***@m8solutions.dk> wrote in message
news:1159279826.720431.178200@m73g2000cwd.googlegroups.com...
>
> Depending on your case, I guess you can profit from regex? e.g.:
>
> dim re as regex = new regex(pattern, regexoptions.multiline) ' e.g.
> pattern = "x"
> dim myMatches as matchcollection = re.matches(source-string) 'e.g.
> source-string = "xxooxoxoxo"
>
> myMatches(n) = n'th occurrence of some match 'e.g. myMatches(2) = x
> myMatches(n).index = the index of the n'th occ. 'e.g.
> myMatches(2).index = 4
>
> anyway - it's more pretty than writing a recursive instr function, I
> guess...
>
>
>
> Anil Gupte skrev:
>
>> I must have missed it.  It cannot be possible.  Or may be I am living in
>> the
>> past of my lisp programming days.
>>
>> There must be a function to find the nth occurrence of a string within
>> another.  instr only gives you the first one.  Of course I can write a
>> function that recurses n times to get the nth occurrence, but there must
>> be
>> a native version, right?  To clarify, lets say I want the 3rd x in this
>> string:
>>
>> strMine="xxoooxoxoxo"
>>
>> instrNth=(strMine, "x", 3)
>> returning 6
>>
>> Any ideas?
>> --
>> Anil Gupte
>> www.keeninc.net
>> www.icinema.com
>
Author
26 Sep 2006 2:16 PM
Herfried K. Wagner [MVP]
"Anil Gupte" <anil-l***@icinema.com> schrieb:
>I must have missed it.  It cannot be possible.  Or may be I am living in
>the past of my lisp programming days.
>
> There must be a function to find the nth occurrence of a string within
> another.  instr only gives you the first one.  Of course I can write a
> function that recurses n times to get the nth occurrence, but there must
> be a native version, right?  To clarify, lets say I want the 3rd x in this
> string:

I'd use the iterative approach:  Call 'InStr' until either the end of the
string is reached or the n-th occurance has been found.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>