Revision 71724 of String.prototype.split()

  • Revision slug: JavaScript/Reference/Global_Objects/String/split
  • Revision title: split
  • Revision id: 71724
  • Created:
  • Creator: Stav
  • Is current revision? No
  • Comment /* Example: Removing spaces from a string */

Revision Content

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
ECMA Version: ECMA-262 (if separator is a string)
ECMA-262, Edition 3 (if separator is a regular expression)

Syntax

split({{mediawiki.external('<i>separator</i>')}}{{mediawiki.external(', <i>limit</i>')}})

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.

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.

In JavaScript 1.2 or later, split has the following additions:

  • 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 parenthesis cause submatches to be included in the returned array.
  • It can take a limit count so that the resulting array does not include trailing empty elements.
  • If you specify LANGUAGE="JavaScript1.2" in the SCRIPT tag, string.split(" ") splits on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns.

Examples

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) {
   arrayOfStrings = stringToSplit.split(separator)
   document.write ('<P>The original string is: "' + stringToSplit + '"')
   document.write ('<BR>The separator is: "' + separator + '"')
   document.write ("<BR>The array has " + arrayOfStrings.length + " elements: ")

   for (var i=0; i < arrayOfStrings.length; i++) {
      document.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 /

Example: The difference between split in JavaScript 1.2 and other versions

Consider the following script:

<SCRIPT LANGUAGE="JavaScript1.2">
str="She sells seashells \nby the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>

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

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

Without LANGUAGE="JavaScript1.2", this script splits only on single space characters, producing:

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

Example: Removing spaces from a string

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

<SCRIPT>
names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write (names + "<BR>" + "<BR>");
re = /\s+;\s+/;
nameList = names.split (re);
document.write(nameList);
</SCRIPT>

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

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.

<SCRIPT LANGUAGE="JavaScript1.2">
myVar = " Hello World. How are you doing? ";
splits = myVar.split(" ", 3);
document.write(splits)
</SCRIPT>

This script displays the following:

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

See Also

charAt, indexOf, lastIndexOf

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Splits a <code>String</code> object into an array of strings by separating the string into substrings.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/String">String</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.1, NES2.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262 (if separator is a string)<br>
ECMA-262, Edition 3 (if separator is a regular expression)</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
split({{mediawiki.external('&lt;i&gt;separator&lt;/i&gt;')}}{{mediawiki.external(', &lt;i&gt;limit&lt;/i&gt;')}})
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>separator</code> </dt><dd> Specifies the character to use for separating the string. The <code>separator</code> is treated as a string or a regular expression. If <code>separator</code> is omitted, the array returned contains one element consisting of the entire string.  
</dd></dl>
<dl><dt> <code>limit</code> </dt><dd> Integer specifying a limit on the number of splits to be found.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>The <code>split</code> method returns the new array.
</p><p>When found, <code>separator</code> is removed from the string and the substrings are returned in an array. If <code>separator</code> is omitted, the array contains one element consisting of the entire string.
</p><p>In JavaScript 1.2 or later, <code>split</code> has the following additions:
</p>
<ul><li> It can take a regular expression argument, as well as a fixed string, by which to split the object string. If <code>separator</code> is a regular expression, any included parenthesis cause submatches to be included in the returned array.
</li></ul>
<ul><li> It can take a limit count so that the resulting array does not include trailing empty elements.
</li></ul>
<ul><li> If you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>SCRIPT</code> tag, <code>string.split(" ")</code> splits on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns.
</li></ul>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_split"> Example: Using <code>split</code> </h4>
<p>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.
</p>
<pre>function splitString (stringToSplit,separator) {
   arrayOfStrings = stringToSplit.split(separator)
   document.write ('&lt;P&gt;The original string is: "' + stringToSplit + '"')
   document.write ('&lt;BR&gt;The separator is: "' + separator + '"')
   document.write ("&lt;BR&gt;The array has " + arrayOfStrings.length + " elements: ")

   for (var i=0; i &lt; arrayOfStrings.length; i++) {
      document.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)
</pre>
<p>This example produces the following output:
</p>
<pre>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 /
</pre>
<h4 name="Example:_The_difference_between_split_in_JavaScript_1.2_and_other_versions"> Example: The difference between <code>split</code> in JavaScript 1.2 and other versions </h4>
<p>Consider the following script:
</p>
<pre>&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;
str="She sells seashells \nby the\n seashore"
document.write(str + "&lt;BR&gt;")
a=str.split(" ")
document.write(a)
&lt;/SCRIPT&gt;
</pre>
<p>Using <code>LANGUAGE="JavaScript1.2"</code>, this script produces:
</p>
<pre>"She", "sells", "seashells", "by", "the", "seashore"
</pre>
<p>Without <code>LANGUAGE="JavaScript1.2"</code>, this script splits only on single space characters, producing:
</p>
<pre>"She", "sells", , , , "seashells", "by", , , "the", "seashore"
</pre>
<h4 name="Example:_Removing_spaces_from_a_string"> Example: Removing spaces from a string </h4>
<p>In the following example, <code>split</code> looks for 0 or more spaces followed by a semicolon followed by 1 or more spaces and, when found, removes the spaces from the string. <code>nameList</code> is the array returned as a result of <code>split</code>.
</p>
<pre>&lt;SCRIPT&gt;
names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write (names + "&lt;BR&gt;" + "&lt;BR&gt;");
re = /\s+;\s+/;
nameList = names.split (re);
document.write(nameList);
&lt;/SCRIPT&gt;
</pre>
<p>This prints two lines; the first line prints the original string, and the second line prints the resulting array.
</p>
<pre>Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand
</pre>
<h4 name="Example:_Returning_a_limited_number_of_splits"> Example: Returning a limited number of splits </h4>
<p>In the following example, <code>split</code> looks for 0 or more spaces in a string and returns the first 3 splits that it finds.
</p>
<pre>&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;
myVar = " Hello World. How are you doing? ";
splits = myVar.split(" ", 3);
document.write(splits)
&lt;/SCRIPT&gt;
</pre>
<p>This script displays the following:
</p>
<pre>["Hello", "World.", "How"]
</pre>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Objects/String/charAt">charAt</a>,
<a href="en/Core_JavaScript_1.5_Reference/Objects/String/indexOf">indexOf</a>,
<a href="en/Core_JavaScript_1.5_Reference/Objects/String/lastIndexOf">lastIndexOf</a>
</p>
Revert to this revision