JavaScript: How to Check Whether a String Contains a Substring?

JavaScript: How to Check Whether a String Contains a Substring?

When you are doing work with strings in javascript. And you need to manipulate the string. For example, Find the string like substring, get the length of the string, etc. There are many methods in JavaScript for this. By using this you can check whether a string contains a substring in JavaScript.

Through this tutorial, you will learn how to check whether a string contains a character or substrings in javascript using the javaScript include(), indexOf() method.

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

By using the following methods, you can easily check whether a string contains a substring in JavaScript:

  • Method 1: Using the indexOf() Method
  • Method 2: Using the includes() Method
  • Method 3: Using Regular Expressions

Method 1: Using the indexOf() Method

Using the indexOf() method, you check the string containing the substring and this method will return the index of the first occurrence of a substring in a string. If the substring is not found, it returns -1.

For example using this method to check whether a string contains a substring in the following way:

let str = 'Hello, World!';
let substr = 'World';

if (str.indexOf(substr) !== -1) {
    console.log('Substring found!');
} else {
    console.log('Substring not found!');
}

In the above given example, you check whether the string ‘Hello, World!’ contains the substring ‘World’ using the indexOf() method. If the substring is found, you print ‘Substring found!’ to the console; otherwise, you print ‘Substring not found!’.

Method 2: Using the includes() Method

Using the includes() method, you can also check whether a string contains a substring in JavaScript. Becuase this method will return a boolean value indicating whether a string contains a substring.

For example using this method to check whether a string contains a substring in the following way:

let str = 'Hello, World!';
let substr = 'World';

if (str.includes(substr)) {
    console.log('Substring found!');
} else {
    console.log('Substring not found!');
}

This is the example taken. Have also used the example with indexOf() method. It has just used the join() method in place of the indexOf() method. To check whether the string ‘Hello, World!’ contains the substring ‘World’ using the includes() method. If the substring is found, it prints ‘Substring found!’ to the console; otherwise, it prints ‘Substring not found!’.

Method 3: Using Regular Expressions

Also, you can check whether a string contains a substring using regular expressions. The Regular expressions provide a powerful way to search for patterns in strings.

For example, using regular expressions to check whether a string contains a substring in the following way:

let str = 'Hello, World!';
let substr = 'World';
let pattern = new RegExp(substr);

if (pattern.test(str)) {
    console.log('Substring found!');
} else {
    console.log('Substring not found!');
}

In the above example, you create a regular expression pattern using the substring ‘World’ and the RegExp constructor. you then use the test() method to check whether the string ‘Hello, World!’ matches the pattern. If the pattern matches, it prints ‘Substring found!’ to the console; otherwise, it prints ‘Substring not found!’.

Conclusion

In this tutorial, you have learned three different methods to check whether a string contains a substring in JavaScript. The indexOf() and includes() methods are built-in functions that are easy to use and provide a simple way to check for substrings. Regular expressions provide a more powerful and flexible way to search for patterns in strings, but require a bit more knowledge of regular expression syntax. By understanding these methods, you can manipulate strings with greater ease and efficiency in your JavaScript code.

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, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL 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 *