String.prototype.substr()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

String 值的 substr() 方法返回此字符串的一部分,从指定索引开始,然后扩展给定数量的字符。

¥The substr() method of String values returns a portion of this string, starting at the specified index and extending for a given number of characters afterwards.

注意:substr() 不是主要 ECMAScript 规范的一部分 - 它是在 附件 B:适用于 Web 浏览器的附加 ECMAScript 功能 中定义的,对于非浏览器运行时来说,附件 B:适用于 Web 浏览器的附加 ECMAScript 功能 是规范可选的。因此,建议人们使用标准的 String.prototype.substring()String.prototype.slice() 方法来使他们的代码最大限度地跨平台友好。String.prototype.substring() page 对这三种方法进行了一些比较。

¥Note: substr() is not part of the main ECMAScript specification — it's defined in Annex B: Additional ECMAScript Features for Web Browsers, which is normative optional for non-browser runtimes. Therefore, people are advised to use the standard String.prototype.substring() and String.prototype.slice() methods instead to make their code maximally cross-platform friendly. The String.prototype.substring() page has some comparisons between the three methods.

Try it

语法

¥Syntax

js
substr(start)
substr(start, length)

参数

¥Parameters

start

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

length Optional

要提取的字符数。

返回值

¥Return value

包含给定字符串的指定部分的新字符串。

¥A new string containing the specified part of the given string.

描述

¥Description

字符串的 substr() 方法从字符串中提取 length 个字符,从 start 索引开始计数。

¥A string's substr() method extracts length characters from the string, counting from the start index.

  • 如果是 start >= str.length,则返回空字符串。
  • 如果是 start < 0,则索引从字符串末尾开始计数。更正式地说,在本例中子字符串从 max(start + str.length, 0) 开始。
  • 如果省略 startundefined,则将其视为 0
  • 如果省略 lengthundefined,或者如果省略 start + length >= str.length,则 substr() 将字符提取到字符串末尾。
  • 如果是 length < 0,则返回空字符串。
  • 对于 startlengthNaN 被视为 0

尽管我们鼓励你避免使用 substr(),但在遗留代码中,没有简单的方法可以将 substr() 迁移到 slice()substring(),而无需为 substr() 编写填充代码。例如,当 str = "01234", a = 1, l = -2 - substr() 返回空字符串时,str.substr(a, l)str.slice(a, a + l)str.substring(a, a + l) 都会有不同的结果,slice() 返回 "123",而 substring() 返回 "0"。实际的重构路径取决于对 al 范围的了解。

¥Although you are encouraged to avoid using substr(), there is no trivial way to migrate substr() to either slice() or substring() in legacy code without essentially writing a polyfill for substr(). For example, str.substr(a, l), str.slice(a, a + l), and str.substring(a, a + l) all have different results when str = "01234", a = 1, l = -2substr() returns an empty string, slice() returns "123", while substring() returns "0". The actual refactoring path depends on the knowledge of the range of a and l.

示例

¥Examples

使用 substr()

¥Using substr()

js
const aString = "Mozilla";

console.log(aString.substr(0, 1)); // 'M'
console.log(aString.substr(1, 0)); // ''
console.log(aString.substr(-1, 1)); // 'a'
console.log(aString.substr(1, -1)); // ''
console.log(aString.substr(-3)); // 'lla'
console.log(aString.substr(1)); // 'ozilla'
console.log(aString.substr(-20, 2)); // 'Mo'
console.log(aString.substr(20, 2)); // ''

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看