A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/slice below:

String.prototype.slice() - JavaScript | MDN

String.prototype.slice()

Baseline Widely available

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

尝试一下
const str = "The quick brown fox jumps over the lazy dog.";

console.log(str.slice(31));
// Expected output: "the lazy dog."

console.log(str.slice(4, 19));
// Expected output: "quick brown fox"

console.log(str.slice(-4));
// Expected output: "dog."

console.log(str.slice(-9, -5));
// Expected output: "lazy"
语法
slice(indexStart)
slice(indexStart, indexEnd)
参数
indexStart

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

indexEnd 可选

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

返回值

一个包含提取的字符串片段的新字符串。

描述

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

slice() 方法提取直到但不包括 indexEnd 的文本。例如,str.slice(1, 4) 提取的是第二个字符到第四个字符(字符的索引为 1、2 和 3)。

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

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

const str1 = "The morning is upon us."; // str1 的长度是 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()

下面的例子在使用 slice() 时传入了负值作为索引。

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 个字符来找到结束索引。

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

在这个例子中,它通过从字符串开头正数 11 个字符来找到起始索引,并通过从字符串末尾倒数 7 个字符来找到结束索引。

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

这些参数通过从字符串末尾倒数 5 个字符来找到起始索引,并通过从字符串末尾倒数 1 个字符来找到结束索引。

console.log(str.slice(-5, -1)); // "n us"
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4