String.prototype.slice()

  • Revision slug: JavaScript/Reference/Global_Objects/String/slice
  • Revision title: slice
  • Revision id: 13122
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[es:Referencia de JavaScript 1.5:Objetos globales:String:slice]] <<langbot>>

Revision Content

Summary

Extracts a section of a string and returns a new string.

Method of String
Implemented in: JavaScript 1.0, NES2.0
ECMA Version: ECMA-262, Edition 3

Syntax

var sub = string.slice(beginslice[, endSlice]);

Parameters

beginSlice 
The zero-based index at which to begin extraction.
endSlice 
The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.

Description

slice extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.

slice extracts up to but not including endSlice. string.slice(1,4) extracts the second character through the fourth character (characters indexed 1, 2, and 3).

As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.

Examples

Example: Using slice to create a new string

The following example uses slice to create a new string.

// assumes a print function is defined
var str1 = "The morning is upon us.";
var str2 = str1.slice(3, -2);
print(str2);

This writes:

morning is upon u


{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/slice", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/slice", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/slice" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Extracts a section of a string and returns a new string.
</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.0, NES2.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262, Edition 3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">var sub = <i>string</i>.slice(<i>beginslice</i>[, <i>endSlice</i>]);
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>beginSlice</code> </dt><dd> The zero-based index at which to begin extraction. </dd></dl>
<dl><dt> <code>endSlice</code> </dt><dd> The zero-based index at which to end extraction. If omitted, <code>slice</code> extracts to the end of the string.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>slice</code> extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.
</p><p><code>slice</code> extracts up to but not including <code>endSlice</code>. <code>string.slice(1,4)</code> extracts the second character through the fourth character (characters indexed 1, 2, and 3).
</p><p>As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_slice_to_create_a_new_string"> Example: Using <code>slice</code> to create a new string </h4>
<p>The following example uses <code>slice</code> to create a new string.
</p>
<pre class="eval">// assumes a print function is defined
var str1 = "The morning is upon us.";
var str2 = str1.slice(3, -2);
print(str2);
</pre>
<p>This writes:
</p>
<pre class="eval">morning is upon u
</pre>
<p><br>
</p>
<div class="noinclude">
</div>
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/slice", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/slice", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/slice" } ) }}
Revert to this revision