JavaScript split() Method
This tutorial introduces the split() method, from the JavaScript String object. One of the most versatile string tools, the split() method allows you to convert any string into an array, based on a string delimiter passed as argument.
The "sibling" method to split() on the array side is the Array join() method, which takes a string as argument to join array elements together as a string.
Using the split() Method with One Argument
The split() method is applied on a string, takes at least a string delimiter as argument, and the output is an array. If the string delimiter passed to split() is not found in the original string, you will end up with single-element string array (where the single element is your original string).
The split() method returns an array without modifying your original string.
var carter = new String("I learn JavaScript.");
// Use space character to delimit array elements
var arlene = carter.split(" ");
// Display one array element per line
alert( arlene.join("\n") );
Our array has been created by the split() method, and we have as many array elements as split() encountered space characters:
the split() Method and Unmatched String Delimiters
The script below is identical, but uses a delimiter that does not appear in our original string. The split() method will be unable to split our string.
var carter = new String("I learn JavaScript.");
var arlene = carter.split("mouse");
// One array element per line
alert( arlene.join("\n") );
This time, the split() method returned a one-element string array, since no match was found for the splitter string (delimiter)...
Passing an Empty String to the split() Method
Passing an empty string to the split() method will not generate an error: instead, split() will return an array with as many elements as there are characters in the original string (rationale: there is an empty string between each character).
var carter = "test";
// Pass an empty string as argument
alert( carter.split("").join("\n") );
And the array generated by the split() method counts four elements: (the join() method was used to display each array element on its own line)
The split() method requires at least one argument.
Passing an empty string (a string of zero length) is not the same as passing no argument at all (which amounts to passing a null
value as argument).
Using the split() Method with Two Arguments
The split() method accepts a second, optional argument, used to limit the number of array elements generated in the process. By default (in absence of a second argument) split() will match as many characters as possible.
var carter = new String("I learn JavaScript.");
var arlene = carter.split(" ",2);
alert( arlene.join("\n") );
With the exception of the second argument we passed to the split() method, this script is identical to the first. But by passing a limiter, the result differs:
Passing a limiter larger than the number of elements split() would yield is the same as passing none.
Test the split() Methodd
Interactively test the split() method by editing the JavaScript code below and clicking the Test split() Method button.
Browser support for JavaScript split() method | |||
---|---|---|---|