Logo

Programming-Idioms

History of Idiom 39 > diff from v29 to v30

Edit summary for version 30 by programming-idioms.org:
[Lua] 2 ways => 2 impls

Version 29

2016-11-11, 20:05:17

Version 30

2016-11-11, 20:07:45

Idiom #39 Check if string contains a word

Set boolean ok to true if string word is contained in string s as a substring, or to false otherwise.

Idiom #39 Check if string contains a word

Set boolean ok to true if string word is contained in string s as a substring, or to false otherwise.

Code
ok = s:find(word, 1, true)
-- or
ok = false
if s:find(word, 1, true) then ok = true end
Code
ok = s:find(word, 1, true)
Comments bubble
The first version sets ok to the starting index of the first occurence of word, or if there is no occurence, sets ok to nil. These results are truey and falsey respectively, but do not equate directly with true and false. If you need ok == true or false, then use the second version.
Comments bubble
This sets ok to the starting index of the first occurrence of word, or nil if there is no occurrence. These results are truey and falsey respectively.

If you need ok to equate true or false, then see other implementation.