English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
slice()extracts a part of the string using the method and returns it as a new string without modifying the original string.
instartandendThe parameter specifies the part of the string to be extracted (endis not included).
The index of the first character is 0, the index of the second character is1and so on.
If omittedendIf the parameter is provided, this method will cut out the rest of the string.
Use negative indices to extract from the end of the string (see the 'More Examples' below).
string.slice(start, end)
var str = 'www.oldtoolbag.com'; var ext = str.slice(3);Test and See‹/›
All browsers fully support the slice() method:
Method | |||||
slice() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
start | Required) The index starting from zero, from which the extraction begins |
end | Optional) The index starting from zero, at which the extraction terminates. The character at this index is not included. If omittedendIf end is omitted, slice() extracts to the end of the string. |
Return Value: | A new string containing the extracted part of the string |
---|---|
JavaScript Version: | ECMAScript 1 |
The following example uses slice() to extract positions3to9(10-1of the character: :
var str = 'www.oldtoolbag.com'; var ext = str.slice(3, 10);Test and See‹/›
The following example uses slice() to extract only the last character:
var str = 'www.oldtoolbag.com'; var ext = str.slice(-1);Test and See‹/›
The following example uses slice() with negative indices:
var str = 'www.oldtoolbag.com'; var ext = str.slice(-8, -3);Test and See‹/›