Logo

Programming-Idioms

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

Idiom #133 Case-insensitive string contains

Set boolean ok to true if string word is contained in string s as a substring, even if the case doesn't match, or to false otherwise.

local ok=string.find(string.lower(s),string.lower(word),nil,true)~=nil

Disable search pattern by setting fourth param to true.
ok = string.find(string.lower(s), string.lower(word)) and true or false

string.find(s,p) returns a number, not a boolean.
The operators and and or are used to clean the result
#include <algorithm>
#include <cctype>
auto ok = std::search(std::begin(s), std::end(s), std::begin(word), std::end(word),
    [](auto c, auto d){
        return std::tolower(c) == std::tolower(d);
    }
) != std::end(s);

New implementation...