🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
includes, indexOf, search, and RegExp.Table of Contents
- Introduction
- Using
includesMethod - Using
indexOfMethod - Using
searchMethod - Using Regular Expressions
- Conclusion
Introduction
Strings in JavaScript are a sequence of characters used to represent text. Checking if a string contains a specific substring can be done using various methods, each suited to different scenarios.
Using includes Method
The includes method determines whether one string may be found within another string, returning true or false as appropriate.
Syntax
string.includes(substring)
Example
const str = "Hello, world!";
const substr1 = "world";
const substr2 = "JavaScript";
console.log(str.includes(substr1)); // true
console.log(str.includes(substr2)); // false
Using indexOf Method
The indexOf method returns the index within the calling string object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
Syntax
string.indexOf(substring) !== -1
Example
const str = "Hello, world!";
const substr1 = "world";
const substr2 = "JavaScript";
console.log(str.indexOf(substr1) !== -1); // true
console.log(str.indexOf(substr2) !== -1); // false
Using search Method
The search method executes a search for a match between a regular expression and this String object.
Syntax
string.search(substring) !== -1
Example
const str = "Hello, world!";
const substr1 = "world";
const substr2 = "JavaScript";
console.log(str.search(substr1) !== -1); // true
console.log(str.search(substr2) !== -1); // false
Using Regular Expressions
Regular expressions (RegExp) can be used for more complex pattern matching. The test method executes a search for a match between a regular expression and a specified string. Returns true or false.
Syntax
new RegExp(substring).test(string)
Example
const str = "Hello, world!";
const substr1 = "world";
const substr2 = "JavaScript";
console.log(new RegExp(substr1).test(str)); // true
console.log(new RegExp(substr2).test(str)); // false
Conclusion
Checking if a string contains a substring in JavaScript can be accomplished using various methods, including includes, indexOf, search, and regular expressions. Each method has its own advantages and specific use cases:
- The
includesmethod is straightforward and easy to use. - The
indexOfmethod is useful if you also need the position of the substring. - The
searchmethod is useful when working with regular expressions. - Regular expressions are powerful and flexible for complex pattern matching.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with strings in JavaScript.
Comments
Post a Comment
Leave Comment