Answers for "check if string contains another string"

PHP
3

if str contains jquery

if (str.indexOf("Yes") >= 0)
  
  //case insensitive version
  if (str.toLowerCase().indexOf("yes") >= 0)
Posted by: Guest on July-01-2020
3

find a string contain in another string

#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'
if [[ "$STR" == *"$SUB"* ]]; then
  echo "It's there."
fi
Posted by: Guest on June-29-2020
2

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';
}
Posted by: Guest on May-18-2020
1

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))
Posted by: Guest on March-04-2021

Code answers related to "check if string contains another string"

Browse Popular Code Answers by Language