substr和substring都用来截取字符串
当都只有一个参数时
let a = 'abcdefghijklmnopqrst' const b = a.substr(1) const c = a.substring(1) console.log(b); console.log(c);
两者用法相近
当两个参数时
let a = 'abcdefghijklmnopqrst' const b = a.substr(1,5) const c = a.substring(1,5) console.log(b); console.log(c);
- substr第一个参数为开始截取的位置,第二个参数为截取的长度
- substring第一个参数为开始截取的位置,第二个参数为结束截取的位置