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

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.

var ok = s.contains(word);
with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
Ok : Boolean := Index (S, Word) > 0;
#include <string.h>
int ok=strstr(s,word)!=NULL;
#include<stdio.h>
#include<conio.h>
#include<string.h>
 
 
int main()

{
char str[30]=”www.cprogrammingexpert.com”;
clrscr();
printf(“\n str = %s” , str);
printf(“\n strlen(str)= %d \n”,strlen(str));
getch();
return 0;
}
var ok = s.Contains(word);
import std.algorithm.searching;
bool ok = s.canFind(word);
ok = String.contains?(s, word)
Ok = string:str(S, Word) > 0.
import "strings"
ok := strings.Contains(s, word)
ok = word `isInfixOf` s
var ok = s.includes(word);
var ok = s.indexOf(word) !== -1;
boolean ok = s.contains(word);
ok = s:find(word, 1, true) ~= nil
ok = s:find(word, 1, true)
ok = false
if s:find(word, 1, true) then ok = true end
$ok = strpos($s, $word) !== false;
ok := pos(word,s)<>0;
$ok = index($s,$word) >= 0;
ok = word in s
ok = s.include?(word)
let ok=s.contains(word);
val ok = s.contains(word)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms