Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lua

Idiom #39 Check if string contains a word

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

ok = s:find(word, 1, true) ~= nil

This checks that the found index is not nil.
ok = s:find(word, 1, true)

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.
ok = false
if s:find(word, 1, true) then ok = true end
with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
Ok : Boolean := Index (S, Word) > 0;

New implementation...
< >
programming-idioms.org