JS字符串截取函数slice()、substring()、substr()的区别
警告:虽然 String.prototype.substr(...) 并未严格弃用(如“从 Web 标准中删除”),但它被视为遗留函数javascript 截取 字符串,应尽可能避免。 它不是 JavaScript 核心语言的一部分,将来可能会被删除。 如果可以的话,请使用 substring() 代替。
JS中slice()、substring()、substr()都具有截取字符串的功能,那么它们的用法有什么区别呢? 如果你也有疑问,这篇文章似乎能够帮助你。
另外,这三个方法不会影响切割后的原字符串,并且都有返回值
重要提示:与 slice() 和 substr() 方法不同javascript 截取 字符串,substring() 不接受负参数。
1. 子串()
substring() 方法返回一个索引和另一个索引之间的字符串,语法如下:
str.substring(indexStart, [indexEnd])
这里有六点需要注意:
这是一些示例代码:
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()
substr()方法返回字符串中从指定位置开始指定个数的字符,句型如下:
str.substr(start, [length])
下面有四点需要注意:
这是一些示例代码:
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): '
需要注意的是,微软的JScript不支持起始索引为负值。 如果您想使用此功能,可以使用以下兼容性代码来解决此错误:
// 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);
}
3. substring() 和 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])
下面有三点需要注意:
这是一些示例代码:
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): '