🎓 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment