ADVERTISEMENT
ADVERTISEMENT

Check whether a string contains a substring using includes() and indexOf() methods in JavaScript

Given a string and a substring, we have to check whether the given string contains the substring.
Submitted by Pratishtha Saxena, on May 18, 2022

Suppose we have a string "My name is Tom". A string is a combination of different substrings. There are many substrings in this given string. To check whether a specified substring is present in this string or not, let's discuss the following methods.

  1. Using includes() method
  2. Using indexOf() method

1) Using includes() Method

In JavaScript, includes() method checks whether a sub-string or a character is present in the string or not. It will return output in terms of true and false. This method is case sensitive, which means that it will consider uppercase and lowercase differently.

Syntax:

string.includes("SubString");

Let's understand this with examples.

Example 1:

str = "My name is Tom";
check = str.includes("ame");
console.log(check);

Output:

true

Example 2:

str = "My name is Tom";
check = str.includes("tom");
console.log(check);

Output:

false

2) Using indexOf() Method

As the name indicates, indexOf() will return the index of the value or character specified inside it. If a substring is specified then it will return the index if the starting of the substring.

Also, just to check whether the substring is present in the string or not then we can apply a condition. If the index of the sub-string is not equals to (-1) then obviously it is present in the given string and hence it will return true, if not then it will return false.

Example 3:

str = "My name is Tom";
subString = str.indexOf("ame i");
console.log(subString);

Output:

4

Example 4:

str = "My name is Tom";
subString = str.indexOf("Tom") !== -1;
console.log(subString);

Output:

false

JavaScript Examples »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.