快速区分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
    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)

原文链接:快速区分slice()、substring()、substr()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容