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 standardString.prototype.substring()
andString.prototype.slice()
methods instead to make their code maximally cross-platform friendly. TheString.prototype.substring()
page has some comparisons between the three methods.
Try it
语法
参数
返回值
描述
¥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)
开始。 - 如果省略
start
或undefined
,则将其视为0
。 - 如果省略
length
或undefined
,或者如果省略start + length >= str.length
,则substr()
将字符提取到字符串末尾。 - 如果是
length < 0
,则返回空字符串。 - 对于
start
和length
,NaN
被视为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"
。实际的重构路径取决于对 a
和 l
范围的了解。
¥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 = -2
— substr()
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
.
示例
使用 substr()
¥Using substr()
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 |
浏览器兼容性
BCD tables only load in the browser