美文网首页
substring和substr的区别

substring和substr的区别

作者: 抽疯的稻草绳 | 来源:发表于2021-01-29 09:12 被阅读0次
两者都是截取字符串。
相同点:如果只是写一个参数,两者的作用都一样:都是是截取字符串从当前下标以后直到字符串最后的字符串片段。
var str = '123456789';
console.log(str.substr(2));    //  "3456789"
console.log(str.substring(2)) ;//  "3456789"
不同点:第二个参数

substr(startIndex,lenth): 第二个参数是截取字符串的长度(从起始点截取某个长度的字符串);

substring(startIndex, endIndex): 第二个参数是截取字符串最终的下标 (截取2个位置之间的字符串,‘含头不含尾’)。

console.log("123456789".substr(2,5));    //  "34567"
console.log("123456789".substring(2,5)) ;//  "345"

相关文章

网友评论

      本文标题:substring和substr的区别

      本文链接:https://www.haomeiwen.com/subject/uvyazktx.html