Check If One String Contains Another Substring

For More Videos Visit Our YouTube Channel




Here we consider how to check if one string contains another substring. Lets start with one example here. Suppose we have to search for a string blur within sky blue.


          var parentString = "sky blue",
          var substring = "blur";

         if( parentString.indexOf( substring ) == -1 ){
                   // Substring Is Not Present In parent String.
          }

         if( parentString.indexOf( substring ) > -1 ){
                   // Substring Is Present In parent String.
          }




Here we have to make use of String.prototype.indexOf().


The indexof() method returns the first occurrence of substring within the parent String.
If indexof() method returns -1, the Substring is not present inside the parent String.
The indexOf() method is case sensitive.


          var parentString = "sky blue",
          var substring = "Blue";

         parentString.indexOf( substring ) will be -1



By this way we can check if one string contains another substring.