JavaScript参考手册
字符串 | String
string.substr
substr()
方法返回一个字符串中从指定位置开始到指定字符数的字符。
语法
str.substr(start, [length])
参数
start
开始提取字符的位置。如果为负值,则被看作 strLength + start,
其中strLength
为字符串的长度(例如,如果 start
为-3,则被看作strLength + (-3))。length
可选。提取的字符数。
返回值
包含给定字符串的提取部分的新字符串。如果length
是0或负数,则返回空字符串。
描述
start
是一个字符的索引。首字符的索引为 0,最后一个字符的索引为 字符串的长度减去1。substr
从start
位置开始提取字符,提取length
个字符(或直到字符串的末尾)。
如果start
为正值,且大于或等于字符串的长度,则 substr
返回一个空字符串。
如果 start
为负值,则 substr
把它作为从字符串末尾开始的一个字符索引。如果 start
为负值且 abs(start)
大于字符串的长度,则 substr
使用 0 作为开始提取的索引。注意负的 start
参数不被 Microsoft JScript 所支持。
如果 length
为 0 或负值,则 substr
返回一个空字符串。如果忽略length
,则substr
提取字符,直到字符串末尾。
示例
使用substr()
var str = 'abcdefghij';
console.log('(1, 2): ' + str.substr(1, 2)); // '(1, 2): bc'
console.log('(-3, 2): ' + str.substr(-3, 2)); // '(-3, 2): hi'
console.log('(-3): ' + str.substr(-3)); // '(-3): hij'
console.log('(1): ' + str.substr(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): ab'
console.log('(20, 2): ' + str.substr(20, 2)); // '(20, 2): '
备注
Microsoft's JScript 不支持负的 start 索引。如果你想充分利用该方法的功能,则需要使用下面的兼容性代码修复此 bug:
// only run when the substr() function is broken
if ('ab'.substr(-1) != 'b') {
/**
* Get the substring of a string
* @param {integer} start where to start the substring
* @param {integer} length how many characters to return
* @return {string}
*/
String.prototype.substr = function(substr) {
return function(start, length) {
// call the original method
return substr.call(this,
// did we get a negative start, calculate how much it is from the beginning of the string
// adjust the start parameter for negative value
start < 0 ? this.length + start : start,
length)
}
}(String.prototype.substr);
}
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) |
Standard |
Defined in the (informative) Compatibility Annex B. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262)The definition of 'String.prototype.substr' in that specification. |
Standard |
Defined in the (informative) Compatibility Annex B |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype.substr' in that specification. |
Standard |
Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers |
ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype.substr' in that specification. |
Living Standard |
Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers |
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
字符串 | String相关
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络