Logo

Programming-Idioms

History of Idiom 39 > diff from v20 to v21

Edit summary for version 21 by :
New Lua implementation by user [jparoz]

Version 20

2015-12-18, 03:10:03

Version 21

2016-02-16, 17:16:27

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
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.