🎓 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
Introduction
Splitting a string into an array of words is a common operation in JavaScript. This can be easily achieved using the split() method. This guide will walk you through writing a JavaScript program to split a string into an array of words based on spaces or other delimiters.
Problem Statement
Create a JavaScript program that:
- Accepts a string.
- Splits the string into an array of words.
- Displays the array of words.
Example:
Input:
"Hello world, welcome to JavaScript!"Output:
["Hello", "world,", "welcome", "to", "JavaScript!"]Input:
"Split this string into words"Output:
["Split", "this", "string", "into", "words"]
Solution Steps
- Initialize the String: Provide a string of text that needs to be split.
- Use the
split()Method: Split the string into an array using a space (' ') or other delimiters as the separator. - Display the Result: Output the array of words.
JavaScript Program
// JavaScript Program to Split a String into an Array of Words
// Author: https://www.rameshfadatare.com/
function splitStringIntoWords(str) {
// Step 1: Split the string by spaces
const wordsArray = str.split(' ');
// Step 2: Display the array of words
console.log("Array of words:", wordsArray);
}
// Example usage
const sentence = "Hello world, welcome to JavaScript!";
splitStringIntoWords(sentence);
Explanation
Step 1: Split the String by Spaces
- The
split(' ')method splits the string into an array of words based on spaces. Each word, including punctuation, becomes a separate element in the resulting array.
Step 2: Display the Array of Words
- The resulting array is printed using
console.log(). Each word is separated by a comma in the output.
Output Example
Array of words: ["Hello", "world,", "welcome", "to", "JavaScript!"]
Example with Different Input
const sentence = "Split this string into words";
splitStringIntoWords(sentence);
Output:
Array of words: ["Split", "this", "string", "into", "words"]
Handling Multiple Spaces or Other Delimiters
You can handle multiple spaces or other delimiters (like commas or periods) by splitting with a regular expression that matches spaces and other characters.
Example: Split by Multiple Spaces or Punctuation
function splitStringByMultipleDelimiters(str) {
// Step 1: Split the string by spaces, commas, and other delimiters
const wordsArray = str.split(/\s+|,|!|\./);
// Step 2: Display the array of words
console.log("Array of words (with multiple delimiters):", wordsArray);
}
// Example usage
const sentence = "Hello, world! Welcome to JavaScript.";
splitStringByMultipleDelimiters(sentence);
Output for Multiple Delimiters
Array of words (with multiple delimiters): ["Hello", "world", "Welcome", "to", "JavaScript", ""]
Note: You may need to filter out empty strings ("") if unwanted.
Conclusion
This JavaScript program demonstrates how to split a string into an array of words using the split() method. The basic approach uses spaces as the delimiter, but by using regular expressions, you can handle multiple spaces or other delimiters like punctuation. This exercise is valuable for text processing and string manipulation 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