Logo

Programming-Idioms

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

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.

with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
Ok : Boolean := Index (S, Word) > 0;
#include <string.h>
int ok = strstr(s,word) != NULL;
(require '[clojure.string :as string])
(def ok (string/includes? word s))
#include <string>
bool ok = s.find(word) != std::string::npos;
var ok = s.Contains(word);
import std.algorithm.searching;
bool ok = s.canFind(word);
var ok = s.contains(word);
ok = String.contains?(s, word)
Ok = string:str(S, Word) > 0.
ok = index(string, 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);
val ok = s.contains(word)
val ok = word in s
(setf ok (search word s))
ok = s:find(word, 1, true)
ok = false
if s:find(word, 1, true) then ok = true end
ok = s:find(word, 1, true) ~= nil
@import Foundation;
BOOL ok=[s containsString:word];
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Nótese el uso de ===. Puesto que == simple no funcionará como se espera
// porque la posición de 'a' está en el 1° (primer) caracter.
if ($pos === false) {
    echo "La cadena '$findme' no fue encontrada en la cadena '$mystring'";
} else {
    echo "La cadena '$findme' fue encontrada en la cadena '$mystring'";
    echo " y existe en la posición $pos";
}
$ok = str_contains($s, $word);
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Nótese el uso de ===. Puesto que == simple no funcionará como se espera
// porque la posición de 'a' está en el 1° (primer) caracter.
if ($pos === false) {
    echo "La cadena '$findme' no fue encontrada en la cadena '$mystring'";
} else {
    echo "La cadena '$findme' fue encontrada en la cadena '$mystring'";
    echo " y existe en la posición $pos";
}
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)
ok := s includesSubstring: word.
Dim ok As Boolean = s.Contains(word)

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