js中slice()和substring()的区别
var s = new String("Couch potato");
相同点:
>>>s.slice(1,5);
"ouch"
>>>s.substring(1,5)
"ouch"
不同点:
>>> s.slice(1, -1);
"ouch potat"
>>> s.substring(1, -1);
"C"
s.slice(1, -1) == slice(1, s.length - 1)
s.substring(1, -1) == substring(1, 0)
The difference between these two methods is how they treat negative arguments. substring() treats them as zeros, while slice() adds them to the length of the string. So if you pass parameters (1, -1) it’s the same as substring(1, 0) and slice(1, s.length - 1):