JavaScript String Contains Substring

JavaScript String Contains Substring

In this tutorial, you will learn how to check whether a string contains substring characters in JavaScript.

Here, we will take several examples of how to check whether a string contains a character or substrings in javascript using the js include() method.

First of all, you should know about the js include method() and, how it works.

JavaScript includes() Method

JavaScript includes() method check whether a string contains the given characters(substring) within is exist or not.

When you use this method, Please remember some points

1. When you use this method, it will returns true in result if the string contains the characters(substring), otherwise, it will returns false in result.

2. Basically, the javaScritp includes() method is case sensitive. For example, it will treat the Uppercase characters and Lowercase characters differently.

Syntax

The following illustrates the syntax of the include() method.

string.includes(searchvalue, start);

Parameters of this method

  • Search Value: This is the string that will contain the search.
  • Start: This is where the search will be processed

If the string has a substring, JavaScript provides several methods to perform the check. Learn the easy way to find a given substring or character in a string.

Examples: To check whether a string contains a substring or character in JavaScript using the include() method.

Example-1

Let’s take first example, we have a string like “This is a tutsmake.com and this tutorial contains javascript include() method examples”, you want to find out in this string “contains” character. So let’s see the below example for that:

var string = "This is a tutsmake.com and this tutorial contains javascript include() method examples."

str.includes("contains");

//The output of this

  true

Demonstration: The include() method finds the “contains” character in whole string, it will return a true.

Below the source code provided for this above example is

<html>
<head>
<title>How to check whether a string contains a substring in JavaScript?</title>
</head>
<body>

<p id="show"></p> 

<script> 
  
    var str = "This is a tutsmake.com and this tutorial contains javascript include() method examples. To check whether a string contains a substring in JavaScript?"; 

    var result = str.includes("contains"); 

    document.getElementById("show").innerHTML = result; 
  
</script> 

</body>
</html>


Output: true

Example – 2

How to check whether a string contains a substring in JavaScript?

The example-2 is linked to StackOverflow

Example-3

Let’s take the third example that accepts an optional parameter for searching the substring or character in the given string.

javaScript includes() method also accepts an optional parameter, an integer which indicates the position where to start searching for:

 var string = "This tutorial contains javascript include() method examples."

 str.includes("contains", 5);

 //The output of this

 true

Below the source code provided for this above example is

<html>
<head>
<title>How to check whether a string contains a substring in JavaScript?</title>
</head>
<body>

<p id="show"></p> 

<script> 
  
    var str = "This tutorial contains javascript include() method examples. To check whether a string contains a substring in JavaScript?"; 

    var result = str.includes('tutorial', 5); 

    document.getElementById("show").innerHTML = result; 
  
</script> 

</body>
</html>


Output: true

Conclusion

In this JavaScript tutorial – you have learned the easy way to find/check a whether given substring or character in a string.

Recommended JavaScript Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *