浅谈substr(),substring(),slice()的用法区别

1 三者区别

  • substr(start,length)返回从指定下标开始的长度为length字符,可以为负数
  • substring(start,end)返回指定下标间的字符,包含start,不包含end
  • slice(start,end)返回指定下标间的数组元素,包含start,不包含end
  • substrsubstring是字符串特有的方法,slice既是数组方法,也是字符串方法

2 substring和slice

2.1 substring MDN

在这里插入图片描述

2.2 slice MDN

在这里插入图片描述

2.3 区别

substringslice的主要区别在于,substring任一参数小于0或者为Nan,则当作0。slice的参数为负数,则它表示在原数组中的倒数第几个元素。

3. 举个栗子

var stringValue = "hello world";

console.log(stringValue.slice(3));          //”lo world”
console.log(stringValue.substring(3));      //”lo world”
console.log(stringValue.substr(3));        //”lo world”

console.log(stringValue.slice(3,7));         //”lo w”
console.log(stringValue.substring(3,7));    //”lo w”
console.log(stringValue.substr(3,7));       //”lo worl”

console.log(stringValue.slice(-3));         //"rld" 从后往前数3个开始
console.log(stringValue.substring(-3));     //"hello world" 为负,默认从0开始
console.log(stringValue.substr(-3));        //"rld"

console.log(stringValue.slice(3,-4));       //”lo w” 下标从3开始到-4(从后往前数4个)
console.log(stringValue.substring(3,-4));   //”hel” 
console.log(stringValue.substr(3,-4));      //”” 长度为负,默认不显示

猜你喜欢

转载自blog.csdn.net/Dax1_/article/details/125252032