🎓 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
In this chapter, we will explore the includes() method in TypeScript. This method is a built-in function that helps in determining whether one string contains another substring. Understanding how to use includes() is useful for checking the presence of substrings within strings.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The includes() method determines whether one string contains another substring. It returns true if the substring is found within the string, and false otherwise.
2. Syntax
string.includes(searchString, position?);
Parameters
searchString: The substring to search for within the string.position(optional): The position within the string at which to begin searching forsearchString. Defaults to 0.
Return Value
The method returns a boolean value: true if the searchString is found within the string, otherwise false.
3. Examples
Let's look at some examples to understand how includes() works in TypeScript.
Example 1: Basic Usage
In this example, we check if a string contains a specific substring.
let str: string = "Hello, TypeScript!";
console.log(str.includes("TypeScript")); // Output: true
console.log(str.includes("JavaScript")); // Output: false
Example 2: Using includes() with Position
In this example, we use the position parameter to start the search from a specific index.
let str: string = "Hello, TypeScript!";
console.log(str.includes("Type", 7)); // Output: true
console.log(str.includes("Hello", 1)); // Output: false
Example 3: Case Sensitivity
The includes() method is case-sensitive. In this example, we see how case sensitivity affects the result.
let str: string = "Hello, TypeScript!";
console.log(str.includes("typescript")); // Output: false
console.log(str.includes("TypeScript")); // Output: true
Example 4: Searching for an Empty String
In this example, we search for an empty string within another string.
let str: string = "Hello, TypeScript!";
console.log(str.includes("")); // Output: true
4. Conclusion
In this chapter, we explored the includes() method in TypeScript, which is used to determine whether a string contains another substring. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use includes() effectively can help in various string manipulation tasks in TypeScript, especially when checking for the presence of substrings within strings.
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