Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:String:split

From MDC


Contents

[edit] Summary

Splits a String object into an array of strings by separating the string into substrings.

Method of String
Implemented in: JavaScript 1.1, NES2.0
ECMAScript Version: ECMA-262 (if separator is a string)
ECMA-262, Edition 3 (if separator is a regular expression)

[edit] Syntax

var splits = str.split([separator][, limit]);

[edit] Parameters

separator 
Specifies the character to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string.
limit 
Integer specifying a limit on the number of splits to be found.

[edit] Description

The split method returns the new array.

When found, separator is removed from the string and the substrings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string.

If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array.

Note: When the string is empty, split returns an array containing one empty string, rather than an empty array.

[edit] Examples

[edit] Example: Using split

The following example defines a function that splits a string into an array of strings using the specified separator. After splitting the string, the function displays messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.

function splitString(stringToSplit,separator)
{
  var arrayOfStrings = stringToSplit.split(separator);
  print('The original string is: "' + stringToSplit + '"');
  print('The separator is: "' + separator + '"');
  print("The array has " + arrayOfStrings.length + " elements: ");

  for (var i=0; i < arrayOfStrings.length; i++)
    print(arrayOfStrings[i] + " / ");
}

var tempestString = "Oh brave new world that has such people in it.";
var monthString = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";

var space = " ";
var comma = ",";

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);

This example produces the following output:

The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it. /

The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it. /

The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec /

[edit] Example: Removing spaces from a string

In the following example, split looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split.

var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
print(names);
var re = /\s*;\s*/;
var nameList = names.split(re);
print(nameList);

This prints two lines; the first line prints the original string, and the second line prints the resulting array.

Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand

[edit] Example: Returning a limited number of splits

In the following example, split looks for 0 or more spaces in a string and returns the first 3 splits that it finds.

var myString = "Hello World. How are you doing?";
var splits = myString.split(" ", 3);
print(splits);

This script displays the following:

Hello,World.,How

[edit] Example: Capturing parentheses

If separator contains capturing parentheses, matched results are returned in the array.

var myString = "Hello 1 word. Sentence number 2.";
var splits = myString.split(/(\d)/);
print(splits);

This script displays the following:

Hello ,1, word. Sentence number ,2, .

[edit] See Also

charAt, indexOf, lastIndexOf, Array:join