From 98d160b85e853437cb7892e09e0967534d0b5325 Mon Sep 17 00:00:00 2001 From: Jason Lam Date: Wed, 16 Aug 2023 09:22:10 +0800 Subject: [PATCH] [zh-cn]: Update translation of String.subString() (#15091) Co-authored-by: A1lo --- .../global_objects/string/substring/index.md | 139 +++++++++++------- 1 file changed, 89 insertions(+), 50 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.md b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.md index 65f56a194fd87c..408ba205202adf 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.md @@ -5,20 +5,23 @@ slug: Web/JavaScript/Reference/Global_Objects/String/substring {{JSRef}} -**`substring()`** 方法返回一个字符串在开始索引到结束索引之间的一个子集,或从开始索引直到字符串的末尾的一个子集。 +{{jsxref("String")}} 的 **`substring()`** 方法返回该字符串从起始索引到结束索引(不包括)的部分,如果未提供结束索引,则返回到字符串末尾的部分。 + +{{EmbedInteractiveExample("pages/js/string-substring.html")}} ## 语法 -```plain -str.substring(indexStart[, indexEnd]) +```js-nolint +substring(indexStart) +substring(indexStart, indexEnd) ``` ### 参数 - `indexStart` - - : 需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。 -- `indexEnd` - - : 可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。 + - : 返回子字符串中第一个要包含的字符的索引。 +- `indexEnd` {{optional_inline}} + - : 返回子字符串中第一个要排除的字符的索引。 ### 返回值 @@ -26,71 +29,105 @@ str.substring(indexStart[, indexEnd]) ## 描述 -`substring` 提取从 `indexStart` 到 `indexEnd`(不包括)之间的字符。特别地: +`substring()` 方法从 `indexStart` 开始提取字符,直到(_但不包括_)`indexEnd`。具体来说: + +- 如果省略了 `indexEnd`,则 `substring()` 提取字符直到字符串的末尾。 +- 如果 `indexStart` 等于 `indexEnd`,则 `substring()` 返回一个空字符串。 +- 如果 `indexStart` 大于 `indexEnd`,则 `substring()` 的效果就像交换了这两个参数一样;请参考下面的示例。 -- 如果 `indexStart` 等于 `indexEnd`,`substring` 返回一个空字符串。 -- 如果省略 `indexEnd`,`substring` 提取字符一直到字符串末尾。 -- 如果任一参数小于 0 或为 {{jsxref("NaN")}},则被当作 0。 -- 如果任一参数大于 `stringName.length`,则被当作 `stringName.length`。 -- 如果 `indexStart` 大于 `indexEnd`,则 `substring` 的执行效果就像两个参数调换了一样。见下面的例子。 +任何小于 `0` 或大于 `str.length` 的参数值都会被视为分别等于 `0` 和 `str.length`。 + +任何值为 {{jsxref("NaN")}} 的参数将被视为等于 `0`。 ## 示例 -### 示例:使用 `substring` +### 使用 substring() -下例使用 `substring` 输出字符串 "`Mozilla`" 中的字符: +下例使用 `substring` 输出字符串 `"Mozilla"` 中的字符: ```js -var anyString = "Mozilla"; +const anyString = "Mozilla"; + +console.log(anyString.substring(0, 1)); // 'M' +console.log(anyString.substring(1, 0)); // 'M' + +console.log(anyString.substring(0, 6)); // 'Mozill' + +console.log(anyString.substring(4)); // 'lla' +console.log(anyString.substring(4, 7)); // 'lla' +console.log(anyString.substring(7, 4)); // 'lla' + +console.log(anyString.substring(0, 7)); // 'Mozilla' +console.log(anyString.substring(0, 10)); // 'Mozilla' +``` -// 输出 "Moz" -console.log(anyString.substring(0, 3)); -console.log(anyString.substring(3, 0)); -console.log(anyString.substring(3, -3)); -console.log(anyString.substring(3, NaN)); -console.log(anyString.substring(-2, 3)); -console.log(anyString.substring(NaN, 3)); +### 调用 substring() 时使用 length 属性 -// 输出 "lla" -console.log(anyString.substring(4, 7)); -console.log(anyString.substring(7, 4)); +以下示例使用 `substring()` 方法和 {{jsxref("String/length", "length")}} 属性来提取特定字符串的最后字符。这种方法可能更容易记住,因为你不需要像上面的示例那样知道起始和结束索引。 -// 输出 "" -console.log(anyString.substring(4, 4)); +```js +const text = "Mozilla"; -// 输出 "Mozill" -console.log(anyString.substring(0, 6)); +// 获取字符串的最后 4 个字符 +console.log(text.substring(text.length - 4)); // 打印“illa” -// 输出 "Mozilla" -console.log(anyString.substring(0, 7)); -console.log(anyString.substring(0, 10)); +// 获取字符串的最后 5 个字符 +console.log(text.substring(text.length - 5)); // 打印“zilla” ``` -### 运用 length 属性来使用 substring() +### substring() 和 substr() 之间的区别 + +`substring()` 和 {{jsxref("String/substr", "substr()")}} 方法之间存在细微差别,因此你应该小心不要混淆它们。 + +- `substr()` 方法的两个参数是 `start` 和 `length`,而 `substring()` 方法的参数是 `start` 和 `end`。 +- 如果 `substr()` 的 `start` 索引为负数,它将循环到字符串的末尾,而 `substring()` 会将其限制为 `0`。 +- 在 `substr()` 中,如果长度为负数,将被视为零;而在 `substring()` 中,如果 `end` 小于 `start` ,则会交换这两个索引。 -下面一个例子运用了 String.length 属性去获取指定字符串的倒数元素。显然这个办法更容易记住,因为你不再像上面那个例子那样去记住起始位置和最终位置。 +此外,`substr()` 被认为是 ECMAScript 中的*遗留特性*,因此如果可能的话最好避免使用它。 ```js -// Displays 'illa' the last 4 characters -var anyString = "Mozilla"; -var anyString4 = anyString.substring(anyString.length - 4); -console.log(anyString4); - -// Displays 'zilla' the last 5 characters -var anyString = "Mozilla"; -var anyString5 = anyString.substring(anyString.length - 5); -console.log(anyString5); +const text = "Mozilla"; +console.log(text.substring(2, 5)); // "zil" +console.log(text.substr(2, 3)); // "zil" ``` -### 示例:替换一个字符串的子字符串 +### substring() 和 slice() 之间的区别 -下例替换了一个字符串中的子字符串。可以替换单个字符和子字符串。该例结尾调用的函数将 "`Brave New World`" 变成了 "`Brave New Web`"。 +`substring()` 和 {{jsxref("String/slice", "slice()")}} 方法几乎相同,但在处理负数参数时有一些细微差别。 + +`substring()` 方法在 `indexStart` 大于 `indexEnd` 的情况下会交换它的两个参数,这意味着仍会返回一个字符串。而 {{jsxref("String/slice", "slice()")}} 方法在这种情况下返回一个空字符串。 ```js +const text = "Mozilla"; +console.log(text.substring(5, 2)); // "zil" +console.log(text.slice(5, 2)); // "" +``` + +如果两个参数中的任何一个或两个都是负数或 `NaN`,`substring()` 方法将把它们视为 `0`。 + +```js +console.log(text.substring(-5, 2)); // "Mo" +console.log(text.substring(-5, -2)); // "" +``` + +`slice()` 方法也将 `NaN` 参数视为 `0`,但当给定负值时,它会从字符串的末尾开始反向计数以找到索引。 + +```js +console.log(text.slice(-5, 2)); // "" +console.log(text.slice(-5, -2)); // "zil" +``` + +请参阅 {{jsxref("String/slice", "slice()")}} 页面以获取更多关于负数的示例。 + +### 替换字符串中的子字符串 + +以下示例替换字符串中的子字符串。它可以替换单个字符和子字符串。示例的最后一个函数调用将字符串 `Brave New World` 更改为 `Brave New Web`。 + +```js +// 将字符串 fullS 中的 oldS 替换为 newS function replaceString(oldS, newS, fullS) { - // Replaces oldS with newS in the string fullS - for (var i = 0; i < fullS.length; i++) { - if (fullS.substring(i, i + oldS.length) == oldS) { + for (let i = 0; i < fullS.length; ++i) { + if (fullS.substring(i, i + oldS.length) === oldS) { fullS = fullS.substring(0, i) + newS + @@ -103,7 +140,9 @@ function replaceString(oldS, newS, fullS) { replaceString("World", "Web", "Brave New World"); ``` -需要注意的是,如果 `oldS` 是 `newS` 的子字符串将会导致死循环。例如,尝试把 "Web" 替换成 "OtherWorld"。一个更好的方法如下: +请注意,如果 `oldS` 本身是 `newS` 的子字符串,这可能导致无限循环,例如,假如你尝试在此处用 `"OtherWorld"` 替换 `"World"`。 + +替换字符串的更好方法如下: ```js function replaceString(oldS, newS, fullS) { @@ -111,7 +150,7 @@ function replaceString(oldS, newS, fullS) { } ``` -上面的代码只是子字符串操作的一个例子。如果你需要替换子字符串,更多时候会用到 {{jsxref("String.prototype.replace()")}}。 +上面的代码仅作为子字符串操作的示例。如果你需要替换子字符串,大多数情况下你会想要使用 {{jsxref("String.prototype.replace()")}} 函数。 ## 规范