Logo

Programming-Idioms

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

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.

#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);
ok = s.Contains(word, StringComparison.CurrentCultureIgnoreCase);
bool ok = s.ToLower().Contains(word.ToLower());
import std.string;
ok = indexOf(word, s, CaseSensitive.no) != -1;
bool ok = s.toUpperCase().contains(word.toUpperCase());
ok = s =~ ~r/#{word}/i
Re = re:compile(Word, [caseless, unicode]),
Ok = re:run(S, Re) =/= nomatch.
 function u_i(string, substr)
  character (len=*), intent(in) :: string, substr
  integer :: i,j, c1, c2, u_i
  u_i = 0
  out: do i=1,len(string)-len(substr)+1
   c1 = iachar(string(i:i))
   if (c1 >= iachar('a') .and. c1 <= iachar('z')) c1 = c1 - 32
   do j=0,len(substr)-2
     c2 = iachar(substr(j+1:j+1))
     if (c2 >= iachar('a') .and. c1 <= iachar('z')) c2 = c2 - 32
     if (c1 /= c2) cycle out
   end do
   u_i = i
   return
 end do out
end function u_i

ok = u_i(string, word) /= 0
import "strings"
lowerS, lowerWord := strings.ToLower(s), strings.ToLower(word)
ok := strings.Contains(lowerS, lowerWord)
import Data.Char (toLower)
import Data.List (isInfixOf)
containsIgnoreCase :: String -> String -> Bool
containsIgnoreCase s word = isInfixOf (map toLower word) (map toLower s)
var lowerS = s.toLowerCase();
var lowerWord = word.toLowerCase();
var ok = lowerS.indexOf(lowerWord) !== -1;
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();
ok = s.toLowerCase().contains(word.toLowerCase());
local ok=string.find(string.lower(s),string.lower(word),nil,true)~=nil
ok = string.find(string.lower(s), string.lower(word)) and true or false
$ok = stripos($word, $s) === false;
OK := Pos(LowerCase(AWord), LowerCase(S)) > 0;
my $ok = $s =~ /\Q$word/i;
from re import search, escape
p = '(?i)' + escape(word)
ok = not not search(p, s)
ok = word.lower() in s.lower()
ok = s.match?( /#{word}/i )
let ok = s.to_ascii_lowercase().contains(&word.to_ascii_lowercase());
extern crate regex;
use regex::RegexBuilder;
let re =
    RegexBuilder::new(&regex::escape(word))
    .case_insensitive(true)
    .build().unwrap();

let ok = re.is_match(s);
extern crate regex;
use regex::Regex;
let re = Regex::new(&format!("(?i){}", regex::escape(word))).unwrap();
let ok = re.is_match(&s);

New implementation...