Logo

Programming-Idioms

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

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.

ok = s.toLowerCase().contains(word.toLowerCase());
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
import java.util.regex.Pattern;
Pattern p = compile("(?i)" + quote(word));
boolean ok = p.matcher(s).find();
#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...