Answers for "check if string contains substring"

4

How do I check if a string contains another string in Swift

let string = "hello Swift"
if string.contains("Swift") {
    print("exists")
}
Posted by: Guest on January-30-2020
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
15

string check javascript

if (typeof myVar === 'string') { /* code */ };
Posted by: Guest on July-23-2020
24

check for substring javascript

const string = "javascript";
const substring = "script";

console.log(string.includes(substring));  //true
Posted by: Guest on July-06-2020
56

javascript string contains

var string = "foo",
    substring = "oo";

console.log(string.includes(substring));
Posted by: Guest on December-21-2019
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 substring"

Browse Popular Code Answers by Language