Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 71744 of String.prototype.split()

  • Revision slug: JavaScript/Reference/Global_Objects/String/split
  • Revision title: split
  • Revision id: 71744
  • Created:
  • Creator: Peng
  • Is current revision? No
  • Comment /* Examples */ Syntax cleanup in the JS.

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.

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

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) {
   var 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: 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.

<script type="text/javascript">
var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write(names + "<br>" + "<br>");
var re = /\s*;\s*/;
var 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.

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

This script displays the following:

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

See Also

charAt, indexOf, lastIndexOf, Array:join

{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/String/split", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/split", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/split" } ) }}

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 <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"> regular expression</a>. 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>Note: When the string is empty, <code>split</code> returns an array containing one empty string, rather than an empty array.
</p>
<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) {
   var 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:_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 0 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 type="text/javascript"&gt;
var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write(names + "&lt;br&gt;" + "&lt;br&gt;");
var re = /\s*;\s*/;
var 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>var myString = "Hello World. How are you doing?";
var splits = myString.split(" ", 3);
document.write(splits);
</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>,
<a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/join">Array:join</a>
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/String/split", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/split", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/split" } ) }}
Revert to this revision