xxxxxxxxxx
var str = "We got a poop cleanup on isle 4.";
if(str.indexOf("poop") !== -1){
alert("Not again");
}
//use indexOf (it returns position of substring or -1 if not found)
xxxxxxxxxx
ECMAScript 6 introduced String.prototype.includes:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
xxxxxxxxxx
// Here's what the syntax looks like:
// string.includes(substring, fromIndex)
// Here's an example:
const bio = "I am a web developer";
console.log(bio.includes("web"));
// true
xxxxxxxxxx
const string = "Hello, world!";
const substring = "world";
if (string.includes(substring)) {
console.log("Substring found");
} else {
console.log("Substring not found");
}
xxxxxxxxxx
const string = "javascript";
const substring = "script";
console.log(string.includes(substring));
xxxxxxxxxx
ECMAScript 6 introduced String.prototype.includes:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
xxxxxxxxxx
There is a String.prototype.includes in ES6:
"potato".includes("to")
xxxxxxxxxx
let String = "codegrepper";
let Sub = "grepper";
console.log(String.includes(Sub)); // true