slice()
method extracts a section of a string and returns it as a new string, without modifying the original string.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
str.slice(beginIndex[, endIndex])
beginIndex
The zero-based index at which to begin extraction. If negative, it is treated as
str
.length +
beginIndex
. (For example, if
beginIndex
is
-3
it is treated as
str
.length - 3
.)
若
beginIndex
is greater than or equal to
str
.length
,
slice()
returns an empty string.
endIndex
可选
The zero-based index before which to end extraction. The character at this index will not be included.
若
endIndex
被省略,
slice()
extracts to the end of the string. If negative, it is treated as
str
.length +
endIndex
. (For example, if
endIndex
is
-3
it is treated as
str
.length - 3
.)
A new string containing the extracted section of the string.
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
endIndex
.
str
.slice(1, 4)
extracts the second character through the fourth character (characters indexed
1
,
2
,和
3
).
As an example,
str
.slice(2, -1)
extracts the third character through the second to last character in the string.
slice()
to create a new string
以下范例使用
slice()
to create a new string.
let str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2) // OUTPUT: he morn
console.log(str3) // OUTPUT: morning is upon u
console.log(str4) // OUTPUT: is upon us.
console.log(str5) // OUTPUT: ""
slice()
with negative indexes
以下范例使用
slice()
with negative indexes.
let str = 'The morning is upon us.' str.slice(-3) // returns 'us.' str.slice(-3, -1) // returns 'us' str.slice(0, -1) // returns 'The morning is upon us'
This example counts backwards from the end of the string by
11
to find the start index and forwards from the start of the string by
16
to find the end index.
console.log(str.slice(-11, 16)) // => "is u"
Here it counts forwards from the start by
11
to find the start index and backwards from the end by
7
to find the end index.
console.log(str.slice(11, -7)) // => " is u"
These arguments count backwards from the end by
5
to find the start index and backwards from the end by
1
to find the end index.
console.log(str.slice(-5, -1)) // => "n us"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.slice' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()