Get number of occurrences of one string in another

Mike Bonner bonnmike at gmail.com
Wed Feb 22 22:46:42 EST 2012


There are several ways to do this i'm sure.

If you want to search a field you can use find in a function for the task


-- I put this in a button. Find empty ensures we start at the beginning of
the field.
-- the repeat loop goes until the findit function returns false, adding 1
to the total each successful find.
on mouseUp
   find empty
   repeat while findIt("p",the long id of field 1)
      put the foundchunk & return after msg
      add 1 to tCount
   end repeat
   put tCount after msg
end mouseUp

function findIt tString, tContainer
   find string tString in tContainer
   if the result is not "not found" then
      return true
   else
      return false
   end if
end findIt

Similar could be done with offset(), Loop with offset, using the
startposition from a previous successful offset to designate the starting
point for the next offset until no match is found.

The benefit of offset is that it can be used on any container, I believe
find is only for fields.

I don't think matchtext of matchchunk would work very well for this but I
could be wrong.


On Wed, Feb 22, 2012 at 8:19 PM, David Beck <david at rotundasoftware.com>wrote:

>
> Is there any way, in the native LiveCode language, to check for the number
> of occurrences of one string in another? FOr example:
>
> put substringCount( "p", "apple" )
> // returns 2
>
> just curious
>
> Thanks!
> _______________________________________________
> 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