Skip to content
Advertisement

String match with ‘in’

In the following code, I would like to search a match for elements in a list in a given string.

JavaScript

When I run the code, I see

JavaScript

That means lst1[0] is found in y which results in yes2. That is wrong in my case. x doesn’t start with PLOP3 although it has LOP3 in its name. So, I would like to see yes1, yes4 only. How can I fix that code?

Advertisement

Answer

“That means LOP3 is found in lst1[0] which results in yes2.”

No, lst1[0], 'LOP3', is found in y, 'PLOP3.LUT'. Other way around. That causes it to print yes2. So perhaps you want to reverse that expression, a in b to b in a?

Or perhaps you want the startswith method, e.g. if x.startswith(i1):?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement