3 different ways in Javascript to find if a string contains a substring or not

3 different ways in Javascript to find if a string contains a substring or not :

Finding out if a substring exists in a string or not is required in most development projects. There are a couple of different ways to check this in JavaScript. In this tutorial, we will learn three different ways in Javascript to find if a substring exists in a String or not.

Using indexOf() :

This is the most common and widely used method to check the existence of a substring in a string.indexOf() method returns -1 if a substring doesn’t exist in a string and it returns the index of the substring in the string if it is found. Let’s try to write it down in code :

//1
function doesExist(subString, mainString) {
  //2
  if (mainString.indexOf(subString) >= 0) {
    console.log('"' + subString + '" is available inside "' + mainString + '"');
  } else {
    console.log(
      '"' + subString + '" doesn\'t exist inside "' + mainString + '"'
    );
  }
}

//3
var mainString = "Hello World";

//4
doesExist("Hello", mainString);
doesExist("Universe", mainString);

Explanation :

The commented numbers in the above program denote the step number below :

1. doesExist method takes two parameters. subString is the sub-string and mainString is the main string. It prints out the result based on subString exists in mainString or not.
2. Inside the function, check the value of indexOf method. If it is greater than or equal to 0, means that the substring exists inside the given string. Print out the message to the user accordingly.
3. In this example, our main string is “Hello World”.
4. Finally, call this method with two different words as sub-string to the above main string.

It will print the below output :

"Hello" is available inside "Hello World"
"Universe" doesn't exist inside "Hello World"

javascript check if string contains substring

Using includes() :

Similar to the above example, we can also use includes() method to check if a substring is included in a string or not. It returns true if the substring exists. Else, it returns false. So, we can easily implement this using one if-else like below :

function doesExist(subString, mainString) {
  if (mainString.includes(subString)) {
    console.log('"' + subString + '" is available inside "' + mainString + '"');
  } else {
    console.log(
      '"' + subString + '" doesn\'t exist inside "' + mainString + '"'
    );
  }
}

var mainString = "Hello World";

doesExist("Hello", mainString);
doesExist("Universe", mainString);

As you can see that we are using the same method as the previous one. The only change is in the if-else condition. The output of this program will be the same as the above.
javascript check if string contains substring

Using RegExp :

We can also use a regular expression to check if a substring is in a string or not. It comes with one method test() that can be used for this. It returns a boolean value like includes() method we have tested above. Let’s implement this in code :

function doesExist(subString, mainString) {
  var mainRegExp = RegExp(subString);

  if (mainRegExp.test(mainString)) {
    console.log('"' + subString + '" is available inside "' + mainString + '"');
  } else {
    console.log(
      '"' + subString + '" doesn\'t exist inside "' + mainString + '"'
    );
  }
}

var mainString = "Hello World";

doesExist("Hello", mainString);
doesExist("Universe", mainString);

The output will be the same as above.
javascript check if string contains substring

Conclusion :

We have learnt three different ways to check if a substring is in a string or not in JavaScript. Try to run the examples above and if you have any queries, drop a comment below.

Leave a Reply