DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

JavaScript split string - String into substrings

In this tutorial, we look at how to use JavaScript to split strings. We break down the syntax, parameters to facilitate further understanding.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents

JavaScript split string:

The JavaScript split() string method is used to split a string into an array of substrings. Once split it returns an array containing a substring. However, the split() method does not change the original string.

In order to specify where the string needs to be split, a separator is used. Javascript splits the string on each occurrence of the separator. This way a string can be easily split into substrings.

Code and Explanation:

The code to split a string in is fairly straightforward, let's take a look:

Syntax:

string.split(separator, limit)
Enter fullscreen mode Exit fullscreen mode

Here “string” refers to the string you are looking to split. The other terms are parameters we take a look at below.

Parameters:

  • Separator - Optional. A specific character, regular expression used to split a string. If not passed, the entire string would be returned.
  • Limit - Optional. An integer that specifies the number of splits. Subsequent occurrences of the separator are not included.

Return Value:

It returns an array containing the substrings. If a separator is not passed, the array will contain one element containing the entire string.

JavaScript split string code:

let Flexiple = 'Hire top freelance developers'
let week = 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday'

let flexiplelist = Flexiple.split(" ")
let weeklist = week.split(",")
let flexiplelist2 = Flexiple.split()
let weeklist2 = week.split(",",3)

console.log(weeklist)
console.log(flexiplelist)
console.log(flexiplelist2)
console.log(weeklist2)
Enter fullscreen mode Exit fullscreen mode

As you can see we have defined two strings, the first one is separated by a space and the next string is separated by a comma.

The output for the above code is:

> Array ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
> Array ["Hire", "top", "freelance", "developers"]
> Array ["Hire top freelance developers"]
> Array ["Monday", "Tuesday", "Wednesday"]
Enter fullscreen mode Exit fullscreen mode

For the first two arrays, we have used their respective separators.

For the third array, we have not passed a separator and hence the array contains one element containing the entire string.

And for the final array, we have passed a limit and hence only 3 substrings are returned.

Closing thoughts - JavaScript split string

This method is useful when trying to split a string based on a pattern. However, in case you wish to split it based on an index you can use the slice methods.

You could refer to this in-depth tutorial - JavaScript Slice.

Discussion (0)