check if string contains substring code example

Example 1: How do I check if a string contains another string in Swift

let string = "hello Swift"
if string.contains("Swift") {
    print("exists")
}

Example 2: angular string contains

const string = "foo";
const substring = "oo";

console.log(string.includes(substring));

Example 3: if string javascript

if (typeof myVar === 'string') { /* code */ };

Example 4: if str contains jquery

if (str.indexOf("Yes") >= 0)
  
  //case insensitive version
  if (str.toLowerCase().indexOf("yes") >= 0)

Example 5: How do I check if a string contains a specific word?

$a = 'Hello world?';

if (strpos($a, 'Hello') !== false) { //PAY ATTENTION TO !==, not !=
    echo 'true';
}
if (stripos($a, 'HELLO') !== false) { //Case insensitive
    echo 'true';
}

Example 6: check if string contains substring

Like this:

if (str.indexOf("Yes") >= 0)
...or you can use the tilde operator:

if (~str.indexOf("Yes"))
This works because indexOf() returns -1 if the string wasn't found at all.

Note that this is case-sensitive.
If you want a case-insensitive search, you can write

if (str.toLowerCase().indexOf("yes") >= 0)
Or:

if (/yes/i.test(str))

Tags:

C Example