mozilla

Revision 509181 of String.prototype.split()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/split
  • Revision title: String.prototype.split()
  • Revision id: 509181
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "String")}}

Summary

The split() method splits a String object into an array of strings by separating the string into substrings.

Syntax

str.split([separator][, limit])

Parameters

separator
Specifies the character(s) 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. If separator is an empty string, str is converted to an array of characters.
limit
Integer specifying a limit on the number of splits to be found. The split method still splits on every match of separator, but it truncates the returned array to at most limit elements.

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 an empty string, str is converted to an array of characters.

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. However, not all browsers support this capability.

{{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);

  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 /

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

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

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,.

Specifications

Specification Status Comment
ECMAScript 3rd Edition. Standard Initial definition.
Implemented in JavaScript 1.1
{{SpecName('ES5.1', '#sec-15.5.4.14', 'String.prototype.split')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-string.prototype.split', 'String.prototype.split')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("String.prototype.charAt()")}}
  • {{jsxref("String.prototype.indexOf()")}}
  • {{jsxref("String.prototype.lastIndexOf()")}}
  • {{jsxref("Array.prototype.join()")}}

Revision Source

<p>{{JSRef("Global_Objects", "String")}}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>split()</strong></code> method splits a <code>String</code> object into an array of strings by separating the string into substrings.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>str</em>.split([<em>separator</em>][, <em>limit</em>])</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>separator</code></dt>
 <dd>
  Specifies the character(s) to use for separating the string. The <code>separator</code> is treated as a string or a <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" title="JavaScript/Reference/Global_Objects/RegExp">regular expression</a>. If <code>separator</code> is omitted, the array returned contains one element consisting of the entire string. If <code>separator</code> is an empty string, <code><em>str</em></code> is converted to an array of characters.</dd>
 <dt>
  <code>limit</code></dt>
 <dd>
  Integer specifying a limit on the number of splits to be found. The <code>split</code> method still splits on every match of <code>separator</code>, but it truncates the returned array to at most <code>limit</code> elements.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<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. If <code>separator</code> is an empty string, <code><em>str</em></code> is converted to an array of characters.</p>
<p>If <code>separator</code> is a regular expression that contains capturing parentheses, then each time <code>separator</code> is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.</p>
<p>{{Note("When the string is empty, <code>split</code> returns an array containing one empty string, rather than an empty array.")}}</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_split" name="Example:_Using_split">Example: Using <code>split</code></h3>
<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 class="brush: js">
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 &lt; 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);
</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>
<h3 id="Example:_Removing_spaces_from_a_string" name="Example:_Removing_spaces_from_a_string">Example: Removing spaces from a string</h3>
<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 class="brush: js">
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);
</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>
<h3 id="Example:_Returning_a_limited_number_of_splits" name="Example:_Returning_a_limited_number_of_splits">Example: Returning a limited number of splits</h3>
<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 class="brush: js">
var myString = "Hello World. How are you doing?";
var splits = myString.split(" ", 3);

print(splits);
</pre>
<p>This script displays the following:</p>
<pre>
Hello,World.,How
</pre>
<h3 id="Example:_Capturing_parentheses" name="Example:_Capturing_parentheses">Example: Capturing parentheses</h3>
<p>If <code>separator</code> contains capturing parentheses, matched results are returned in the array.</p>
<pre class="brush: js">
var myString = "Hello 1 word. Sentence number 2.";
var splits = myString.split(/(\d)/);

print(splits);
</pre>
<p>This script displays the following:</p>
<pre>
Hello ,1, word. Sentence number ,2,.
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition.</td>
   <td>Standard</td>
   <td>Initial definition.<br />
    Implemented in JavaScript 1.1</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.14', 'String.prototype.split')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.split', 'String.prototype.split')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("String.prototype.charAt()")}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
 <li>{{jsxref("Array.prototype.join()")}}</li>
</ul>
Revert to this revision