🎓 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 split() method in TypeScript. This method is a built-in function that helps in splitting a string into an array of substrings based on a specified separator. Understanding how to use split() is useful for breaking down strings into manageable parts.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The split() method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern, where the pattern is provided as the first parameter in the method's call.
2. Syntax
string.split(separator, limit?);
Parameters
separator: The pattern describing where each split should occur. The separator can be a string or a regular expression.limit(optional): An integer that specifies the number of splits. Items after the limit will not be included in the array.
Return Value
The method returns an array of strings, split at each point where the separator occurs in the given string.
3. Examples
Let's look at some examples to understand how split() works in TypeScript.
Example 1: Basic Usage
In this example, we split a string using a space as the separator.
let str: string = "Hello TypeScript World";
let result = str.split(" ");
console.log(result); // Output: [ 'Hello', 'TypeScript', 'World' ]
Example 2: Using a Limit
In this example, we split a string using a space as the separator and limit the number of splits.
let str: string = "Hello TypeScript World";
let result = str.split(" ", 2);
console.log(result); // Output: [ 'Hello', 'TypeScript' ]
Example 3: Using a Regular Expression
In this example, we split a string using a regular expression as the separator.
let str: string = "Hello, TypeScript. Welcome to the world of TypeScript!";
let result = str.split(/\s+/);
console.log(result); // Output: [ 'Hello,', 'TypeScript.', 'Welcome', 'to', 'the', 'world', 'of', 'TypeScript!' ]
Example 4: Splitting by a Character
In this example, we split a string using a specific character as the separator.
let str: string = "apple,banana,orange,grape";
let result = str.split(",");
console.log(result); // Output: [ 'apple', 'banana', 'orange', 'grape' ]
Example 5: No Separator Provided
In this example, we call split() without providing a separator, which returns an array with the original string as the single element.
let str: string = "Hello TypeScript World";
let result = str.split();
console.log(result); // Output: [ 'Hello TypeScript World' ]
4. Conclusion
In this chapter, we explored the split() method in TypeScript, which is used to divide a string into an array of substrings based on a specified separator. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use split() effectively can help in various string manipulation tasks in TypeScript, especially when breaking down strings into manageable parts.
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