How best to check if string contains any of a list of substrings?

Alex Tweedly alex at tweedly.net
Thu Nov 1 06:34:24 EDT 2018


Hi Keith,

I think the simplest method would be (almost) your second attempt below. 
However, you were using "*est*" - i.e. with the '*'s before and after 
the phrase, which is what you would need for 'filter' but not for 
'contains'. So it should work with just

> put “my test phrase” into myTestPhrase
> put “est” & cr & “phr” into tSubstrings
>
> repeat for each line L in tSubstrings
> 	if myTestPhrase contains L then answer “true”
> end repeat
There's almost certainly also a way of doing it in a single pass using 
regex - but I'm not brave enough to go there :-)

Are you expecting reallylarge strings to search ?  Or really many 
phrases to search for ?

"beware optimizing something that is just plain fast enough already"

Alex.

On 01/11/2018 09:41, Keith Clarke via use-livecode wrote:
> Folks,
> What is the most efficient way to test whether a variable containing a string such as “my test phrase” contains one of several variable substrings, such as “est” OR “phr” OR... ?
>
> So far I’ve tried (without luck) approaches that simplify into...
>
> put “my test phrase” into myTestPhrase
> put “*est*” & cr & “*phr*” into tSubstrings
>
> if myTestPhrase is among the lines of tSubstrings then answer “true”
>
> repeat for each line L in tSubstrings
> 	if myTestPhrase contains L then answer “true”
> end repeat
>
> Both seem to be failing - do I have the wrong syntax or approach to the problem?
>
> TIA
> Keith
> _______________________________________________
> 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