字符串方法,slice(),substring(), substr()

slice(), substring(), substr();这三个方法都是返回被操作字符串的一个子字符串,就是返回一个新的字符串。
  1. 都是接受一个参数或者两个参数;
  2. 第一个参数是指定字符串的开始位置;
  3. 第二次参数(在指定的情况下)表示字符串到哪里结束;
  4. 如果没有第二个参数,则将字符串结束的未位作为结束位置
    

       

slice()substring()第二次参数指定的是字符串最后一个字符后面的位置;
substr()第二个参数指定返回的字符串个数;
 var string = 'hello world';
 console.log(string.slice(3));  //lo world
 console.log(string.substring(3));  //lo world
 console.log(string.substr(3));  //lo world
 console.log(string.slice(3, 7));  //lo w
 console.log(string.substring(3, 7)); //lo w
 console.log(string.substr(3, 7)); //lo worl

在传递给这些方法的参数为负数的情况下就不一样了

slice() 会将所有的负数于字符串的长度相加
substr() 会将第一个负参数与字符串长度相加,第二个负参数转化为 0
substring() 将所有的负参数转化为 0
var string = 'hello world';  // length = 11
console.log(string.slice(-3));  // rld    slice(8)
console.log(string.substring(-3));  //hello world  substring(0)
console.log(string.substr(-3));  // rld  substr(8)
console.log(string.slice(3, -4));  //lo w slice(3, 7)
console.log(string.substring(3, -4)); //hel   substring(3, 0)
console.log(string.substr(3, -4)); //  ''   substring(3, 0)

 

 
posted @ 2019-08-28 21:56  奔走_在路上  阅读(179)  评论(0编辑  收藏  举报