Friday, January 17, 2025
HomeProgrammingSplitting Strings in JS - javascript

Splitting Strings in JS [duplicate] – javascript

In JavaScript, you can split strings using the split() method. This method divides a string into an array of substrings based on a specified delimiter.

Syntax:

string.split(separator, limit)

separator: The character or regular expression to split the string.

See also  How can I see the changes in a Git commit?

limit: An optional argument to specify the maximum number of splits.

Examples:

1. Splitting by space:

let str = “Hello World”;
let arr = str.split(” “);
console.log(arr); // Output: [“Hello”, “World”]

2. Splitting by a specific character:

let str = “apple,banana,orange”;
let arr = str.split(“,”);
console.log(arr); // Output: [“apple”, “banana”, “orange”]

See also  Map Interface in Java

3. Limit the number of splits:

let str = “one,two,three,four”;
let arr = str.split(“,”, 2);
console.log(arr); // Output: [“one”, “two”]

Use split() to efficiently break down strings in JavaScript based on any delimiter.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x