Javascript - String split() Method

The split() method can split a string into an array of substrings.

Description

The split() method can split a string into an array of substrings.

If an empty string "" is used as the separator, the string is by each character.

The split() method does not change the original string.

Syntax

string.split(separator,limit)

Parameter Values

Parameter Require Description
separator Optional. character or the regular expression to split the string.
limit Optional. An integer that specifies the number of splits, and items after the limit will not be included in the array

Return

An Array, containing the splitted values

Example

Split a string into an array of substrings:

Demo

//display the array values after the split.
var str = "this is a test test test ?";
var res = str.split(" ");
console.log(res);//from  w w w.ja  va2 s  .  c  o  m

//Omit the separator parameter:
var str = "this is a test?";
var res = str.split();
console.log(res);

//Separate each character, including white-space:
var str = "How are you doing today?";
var res = str.split("");
console.log(res);


//Use the limit parameter:
var str = "How are you doing today?";
var res = str.split(" ", 3);
console.log(res);

//Use a letter as a separator:
var str = "this is a test?";
var res = str.split("t");
console.log(res);

Result