Get number of occurrences of one string in another

Bob Sneidar bobs at twft.com
Wed Feb 22 23:03:12 EST 2012


Aye, but after all, technically, for the question how many times does "pp" occur in "pppppp", the correct answer is 3, unless you care about the fact that pString overlaps. In that case the problem becomes more complex. But who would want to know that??

Bob

 
On Feb 22, 2012, at 7:52 PM, Mike Bonner wrote:

> Was a pretty snazzy solution. Only problem I can see is if you were
> searching say for "pp" in a string that has "pppppp" (you get what I mean)
> If you want to match 1 and 2, 2 and 3, 3 and 4, 4 and 5, 5 and 6 it won't
> work but if you want to match and eliminate 1 2, 3 4 etc its dandyfine. So
> it would depend on the desired result.
> 
> On Wed, Feb 22, 2012 at 8:47 PM, Bob Sneidar <bobs at twft.com> wrote:
> 
>> I am everyone's Uncle. Still, no repeat loops needed. See my solution.
>> 
>> Bob
>> 
>> 
>> On Feb 22, 2012, at 7:44 PM, Jerry Jensen wrote:
>> 
>>> The offset() function has a third parameter: chars to skip. So if you
>> call offset in a loop with the third param set to the previous result, and
>> count loops until it returns 0, Bob's your uncle.
>>> 
>>> On Feb 22, 2012, at 7:37 PM, Andrew Kluthe wrote:
>>> 
>>>> function substringCount pChar, pString
>>>>   put 0 into theCount
>>>>   repeat for each char theChar in pString
>>>>        if theChar is pChar then
>>>>             add 1 to theCount
>>>>        end if
>>>>   end repeat
>>>>   return theCount
>>>> end substringCount
>>>> 
>>>> 
>>>> Would work out nicely if there is not built in function for this.
>>>> 
>>>> --
>>>> View this message in context:
>> http://runtime-revolution.278305.n4.nabble.com/Get-number-of-occurrences-of-one-string-in-another-tp4412675p4412707.html
>>>> Sent from the Revolution - User mailing list archive at Nabble.com.
>>>> 
>>>> _______________________________________________
>>>> use-livecode mailing list
>>>> use-livecode at lists.runrev.com
>>>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>> 
>>> 
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list