String.prototype.slice()

String 值的 slice() 方法提取该字符串的一部分并将其作为新字符串返回,而不修改原始字符串。

¥The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.

Try it

语法

¥Syntax

js
slice(indexStart)
slice(indexStart, indexEnd)

参数

¥Parameters

indexStart

要包含在返回的子字符串中的第一个字符的索引。

indexEnd Optional

要从返回的子字符串中排除的第一个字符的索引。

返回值

¥Return value

包含提取的字符串部分的新字符串。

¥A new string containing the extracted section of the string.

描述

¥Description

slice() 从一个字符串中提取文本并返回一个新字符串。对一个字符串中文本的更改不会影响另一字符串。

¥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() 提取直至但不包括 indexEnd。例如,str.slice(4, 8) 提取第五个字符到第八个字符(索引为 4567 的字符):

¥slice() extracts up to but not including indexEnd. For example, str.slice(4, 8) extracts the fifth character through the eighth character (characters indexed 4, 5, 6, and 7):

              indexStart        indexEnd
                  ↓               ↓
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| T | h | e |   | m | i | r | r | o | r |

                  m   i   r   r
                 _______________
                      ↑
                    Result
  • 如果是 indexStart >= str.length,则返回空字符串。
  • 如果是 indexStart < 0,则索引从字符串末尾开始计数。更正式地说,在本例中,子字符串从 max(indexStart + str.length, 0) 开始。
  • 如果 indexStart 被省略、未定义或不能是 转换为数字,则将其视为 0
  • 如果 indexEnd 被省略、未定义或不能是 转换为数字,或者如果 indexEnd >= str.length,则 slice() 提取到字符串末尾。
  • 如果是 indexEnd < 0,则索引从字符串末尾开始计数。更正式地说,在本例中,子字符串以 max(indexEnd + str.length, 0) 结尾。
  • 如果标准化负值后的 indexEnd <= indexStart(即 indexEnd 表示 indexStart 之前的字符),则返回空字符串。

示例

¥Examples

使用 slice() 创建一个新字符串

¥Using slice() to create a new string

以下示例使用 slice() 创建新字符串。

¥The following example uses slice() to create a new string.

js
const str1 = "The morning is upon us."; // The length of str1 is 23.
const str2 = str1.slice(1, 8);
const str3 = str1.slice(4, -2);
const str4 = str1.slice(12);
const str5 = str1.slice(30);
console.log(str2); // he morn
console.log(str3); // morning is upon u
console.log(str4); // is upon us.
console.log(str5); // ""

使用带有负索引的 slice()

¥Using slice() with negative indexes

以下示例使用具有负索引的 slice()

¥The following example uses slice() with negative indexes.

js
const str = "The morning is upon us.";
str.slice(-3); // 'us.'
str.slice(-3, -1); // 'us'
str.slice(0, -1); // 'The morning is upon us'
str.slice(4, -1); // 'morning is upon us'

此示例从字符串末尾向后计数 11 以查找起始索引,并从字符串开头向前计数 16 以查找结束索引。

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

js
console.log(str.slice(-11, 16)); // "is u"

这里,它从开始向前计数 11 以找到开始索引,从结束向后计数 7 以找到结束索引。

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

js
console.log(str.slice(11, -7)); // " is u"

这些参数从末尾向后计数 5 以查找起始索引,并从末尾向后计数 1 以查找结束索引。

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

js
console.log(str.slice(-5, -1)); // "n us"

规范

Specification
ECMAScript Language Specification
# sec-string.prototype.slice

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看