Re: Find the n-th occurance of a sustring within a string
- From: "macropod" <invalid@xxxxxxxxxxxxxxx>
- Date: Sun, 22 Apr 2007 14:42:20 +1000
Hi Earl,
Well spotted. I like your idea. To be on the safe side, one could use a single character that would be most unlikely to appear in any 'normal' text string (eg an ASCII 135: ‡), without the need to concoct an 'unlikely' string - though I do accept that doing so renders false matches much less likely.
Plus, in order to deal with errors, one could return '0', via:
=IF(ISERROR(FIND(A2&"‡"&A3,SUBSTITUTE(A1,A2,A2&"‡"&A3,A3))),0,FIND(A2&"‡"&A3,SUBSTITUTE(A1,A2,A2&"‡"&A3,A3)))
Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------
"Earl Kiosterud" <someone@xxxxxxxxxxx> wrote in message news:%23MSO87HhHHA.4704@xxxxxxxxxxxxxxxxxxxxxxx
That is slick. It can be fooled, however, in the unlikely event any instance of the string you're looking for happens to have the instance number following it. This might happen in numbers combining alpha characters and digits (like part numbers). For example:
A1: CC3C
A2: C
A3: 3
It finds the second C, and returns 2.
You can use this modification:
=FIND(A2&"|"&A3,SUBSTITUTE(A1,A2,A2&"|"&A3,A3))
You use a character known never to appear in the data, such as the pipe character in this example. The added character can be an unlikely string, like !#$%.
=FIND(A2&"!#$%"&A3,SUBSTITUTE(A1,A2,A2&"!#$%"&A3,A3))
This unlikely string occurs only when Sarge is swearing (and beating the stuffing out of Beetle).
--
Earl Kiosterud
www.smokeylake.com
Note: Some folks prefer bottom-posting.
But if they bottom-post to a reply that's
already top-posted, the thread gets messy. When in Rome...
-----------------------------------------------------------------------
"macropod" <invalid@xxxxxxxxxxxxxxx> wrote in message news:%23K9qw$GhHHA.1216@xxxxxxxxxxxxxxxxxxxxxxx
Hi Tom,
Suppose the string you want to search is in A1, the substring you want to find is in A2 and the substring instance you want to find is in A3, then the following formula will return the position where the nth occurrence of the substring being searched for starts:
=FIND(A2&A3,SUBSTITUTE(A1,A2,A2&A3,A3))
Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------
"TomCon" <someone@xxxxxxxxxxx> wrote in message news:3cxWh.992$YI1.87@xxxxxxxxxxxOne can use SEARCH() or FIND() to find a string within another. And, if you wanted to find the first few, you can nest the calls and it probably works ok. But, this has its limits. Lets say you wanted to find the 10th occurance of a particular string within a string, now the formula gets really hairy, if you nest.
Any clever ideas out there to find the n-th occurance of a string within another?
Thanks,
tom
.
- Follow-Ups:
- Re: Find the n-th occurance of a sustring within a string
- From: Earl Kiosterud
- Re: Find the n-th occurance of a sustring within a string
- References:
- Find the n-th occurance of a sustring within a string
- From: TomCon
- Re: Find the n-th occurance of a sustring within a string
- From: macropod
- Re: Find the n-th occurance of a sustring within a string
- From: Earl Kiosterud
- Find the n-th occurance of a sustring within a string
- Prev by Date: Re: Find the n-th occurance of a sustring within a string
- Next by Date: Re: Find the n-th occurance of a sustring within a string
- Previous by thread: Re: Find the n-th occurance of a sustring within a string
- Next by thread: Re: Find the n-th occurance of a sustring within a string
- Index(es):
Relevant Pages
|