字符串的处理

substring() 和 substr()

相同:如果只是写一个参数,两者的作用都一样:都是是截取字符串从当前下标以后直到字符串最后的字符串片段

substr(startIndex);
substring(startIndex);

var str = '12345678'
console.log(str.substr(2))   // '345678'
console.log(str.substring(2))    // '345678'

不同:

substr(startIndex,lenth): 第二个参数是截取字符串的长度(从起始点截取某个长度的字符串)
substring(startIndex, endIndex): 第二个参数是截取字符串最终的下标 (截取2个位置之间的字符串,‘含头不含尾’)

console.log('12345678'.substr(2,5))   // '34567'
console.log('12345678'.substring(2,5))   // '345'

 

posted on 2022-02-16 11:28  一名小学生呀  阅读(30)  评论(0编辑  收藏  举报

导航