split

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

Applies to

String

Syntax

split(separator, limit)

Parameters

separator

(Optional) Specifies the character to use for separating the string. The separator is treated as a string. If separator is omitted, the array returned contains one element consisting of the entire string. The separator can either be a single separator or a multi-character separators.

limit

(Optional) Integer specifying a limit on the number of splits to be found.

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.

It can take a regular expression argument, as well as a fixed string, by which to split the object string. If separator is a regular expression, any included parentheses cause submatches to be included in the returned array.

It can take a limit count so that it won't include trailing empty elements in the resulting array.

Examples

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) {
      arrayOfStrings = stringToSplit.split(separator)
      Console.Write ('<P>The original string is: "' + stringToSplit + '"')
      Console.Write ('<BR>The separator is: "' + separator + '"')
      Console.Write ("<BR>The array has " + arrayOfStrings.length + " elements: ")
      for (var i=0; i < arrayOfStrings.length; i++) {
            Console.Write (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 /

Consider the following script:

str="She sells    seashells \nby   the\n seashore"
Console.Write(str )
a=str.split(" ")
Console.Write(a)

Using LANGUAGE="JavaScript1.2", this script produces

"She", "sells", "seashells", "by", "the", "seashore"

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.

names = "Harry  Trump  ;Fred Barney; Helen   Rigby ; Bill Abel ;Chris Hand ";
Console.Write (names , "    ");
re = /\s*;\s*/;
nameList = names.split (re);
Console.Write(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

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

myVar = "  Hello World. How are you doing?    ";
splits = myVar.split(" ", 3);
Console.Write(splits)

This script displays the following:

["Hello", "World.", "How"]

See also

String.charAt, String.indexOf, String.lastIndexOf