substring()
方法返回一个索引和另外一个索引之间的字符串,语法以下:javascript
str.substring(indexStart, [indexEnd])java
下面有六点须要注意:web
- substring()从提取的字符indexStart可达但不包括 indexEnd
- 若是indexStart 等于indexEnd,substring()返回一个空字符串。
- 若是indexEnd省略,则将substring()字符提取到字符串的末尾。
- 若是任一参数小于0或是NaN,它被视为为0。
- 若是任何一个参数都大于stringName.length,则被视为是stringName.length。
- 若是indexStart大于indexEnd,那么效果substring()就好像这两个论点被交换了同样; 例如,str.substring(1, 0) == str.substring(0, 1)
如下是一些示例代码:网站
var str = 'abcdefghij';
console.log('(1, 2): ' + str.substring(1, 2)); // '(1, 2): b'
console.log('(1, 1): ' + str.substring(1, 1)); // '(1, 1): '
console.log('(-3, 2): ' + str.substring(-3, 2)); // '(-3, 2): ab'
console.log('(-3): ' + str.substring(-3)); // '(-3): abcdefghij'
console.log('(1): ' + str.substring(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substring(-20, 2)); // '(-20, 2): ab'
console.log('(2, 20): ' + str.substring(2, 20)); // '(2, 20): cdefghij'
console.log('(20, 2): ' + str.substring(20, 2)); // '(20, 2): cdefghij'复制代码
substr()
方法返回从指定位置开始的字符串中指定字符数的字符,语法以下:ui
str.substr(start, [length])this
下面有四点须要注意:spa
substr()
会从start
获取长度为length
字符(若是截取到字符串的末尾,则会中止截取)。- 若是
start
是正的而且大于或等于字符串的长度,则substr()
返回一个空字符串。- 若
start
为负数,则将该值加上字符串长度后再进行计算(若是加上字符串的长度后仍是负数,则从0开始截取)。- 若是
length
为0或为负数,substr()
返回一个空字符串。若是length
省略,则将substr()
字符提取到字符串的末尾。
如下是一些示例代码:prototype
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的JScript不支持起始索引的负值。若是要使用此功能,可使用如下兼容性代码来解决此错误:code
// 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);
}复制代码
substring()
方法的参数表示起始和结束索引,substr()
方法的参数表示起始索引和要包含在生成的字符串中的字符的长度,示例以下:索引
var text = 'Mozilla';
console.log(text.substring(2,5)); // => "zil"
console.log(text.substr(2,3)); // => "zil"复制代码
slice()
方法返回一个索引和另外一个索引之间的字符串,语法以下:
str.slice(beginIndex[, endIndex])
下面有三点须要注意:
- 若
beginIndex
为负数,则将该值加上字符串长度后再进行计算(若是加上字符串的长度后仍是负数,则从0开始截取)。- 若是
beginIndex
大于或等于字符串的长度,则slice()
返回一个空字符串。- 若是
endIndex
省略,则将slice()
字符提取到字符串的末尾。若是为负,它被视为strLength + endIndex
其中strLength
是字符串的长度。
如下是一些示例代码:
var str = 'abcdefghij';
console.log('(1, 2): ' + str.slice(1, 2)); // '(1, 2): b'
console.log('(-3, 2): ' + str.slice(-3, 2)); // '(-3, 2): '
console.log('(-3, 9): ' + str.slice(-3, 9)); // '(-3, 9): hi'
console.log('(-3): ' + str.slice(-3)); // '(-3): hij'
console.log('(-3,-1): ' + str.slice(-3,-1)); // '(-3,-1): hi'
console.log('(0,-1): ' + str.slice(0,-1)); // '(0,-1): abcdefghi'
console.log('(1): ' + str.slice(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.slice(-20, 2)); // '(-20, 2): ab'
console.log('(20): ' + str.slice(20)); // '(20): '
console.log('(20, 2): ' + str.slice(20, 2)); // '(20, 2): '复制代码
参考文档MDN web docs
原文地址王玉路的我的网站