Monday, 11 September 2017

javascript - JQuery string contains check




I need to check whether a string contains another string or not?



var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";



Which function do I use to find out if str1 contains str2?


Answer



You can use javascript's indexOf function.





var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

if(str1.indexOf(str2) != -1){
console.log(str2 + " found");
}




No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...