javaScript字符串截取函数substring()、substr()、slice()三者的区别

var str = "qwertyuiop";

1、substring()方法:返回一个索引和另一个索引之间的字符串。

    语法:str.substring(startIndex,endIndex); 

    Notice:↓↓↓↓↓↓↓↓↓↓

  1. substring() 提取字符串从 startIndex 开始到 endIndex,可以不包含 endIndex。
  2. 如果 startIndex 等于 endIndex,substring()则返回一个空字符串。
  3. 如果 endIndex 省略,则将 substring() 字符提取到字符串的末尾。
  4. 如果任一参数小于0或是NaN,它被视为为0。
  5. 如果任何一个参数都大于 str.length,则被视为是 str.length。
  6. 如果 startIndex 于 endIndex,那么效果substring()就好像这两个论点被交换了一样; 例如,str.substring(1, 0) == str.substring(0, 1)

     例: console.log('(1, 2): ' + str.substring(1, 2)); // '(1, 2): w'

             console.log('(1, 1): ' + str.substring(1, 1)); // '(1, 1): '

             console.log('(1): ' + str.substring(1)); // '(1): wertyuiop'

             console.log('(-3, 2): ' + str.substring(-3, 2)); // '(-3, 2): qw'

             console.log('(-3): ' + str.substring(-3)); // '(-3): qwertyuiop'

             console.log('(2, 20): ' + str.substring(2, 20)); // '(2, 20): ertyuiop' 

                  console.log('(20, 2): ' + str.substring(20, 2)); // '(20, 2): ertyuiop'

2、substr()方法:返回从指定位置开始的字符串中指定字符数的字符。

    语法:str.substr(startIndex,[strLength]);

    Notice:↓↓↓↓↓↓↓↓↓↓

  1. substr() 会从 startIndex 获取长度为 strLength 字符(如果截取到字符串的末尾,则会停止截取)。
  2. 如果 startIndex 是正的并且大于或等于字符串的长度,则substr() 返回一个空字符串。
  3. 若 startIndex 为负数,则将该值加上字符串长度后再进行计算(如果加上字符串的长度后还是负数,则从0开始截取)。
  4. 如果 strLength 为0或为负数,substr() 返回一个空字符串。如果 strLength 省略,则将 substr() 字符提取到字符串的末尾。

         例:  console.log('(1, 2): ' + str.substr(1, 2)); // '(1, 2): we' 

                  console.log('(1, 22): ' + str.substr(1, 22)); // '(1, 22): wertyuiop'

                  console.log('(20, 2): ' + str.substr(20, 2)); // '(20, 2): '

                  console.log('(-3, 2): ' + str.substr(-3, 2)); // '(-3, 2): io'

                  console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): qw' 

                  console.log('(2, 0): ' + str.substr(2, 0)); // '(2, 0): '

                  console.log('(2, -3): ' + str.substr(2, -3)); // '(2, -3): '

                  console.log('(1): ' + str.substr(1)); // '(1): wertyuiop'

 

 3、slice()方法:返回一个索引和另一个索引之间的字符串。

    语法:str.slice(startIndex, endIndex);

    Notice:↓↓↓↓↓↓↓↓↓↓

  1. 若 startIndex 为负数,则将该值加上字符串长度后再进行计算(如果加上字符串的长度后还是负数,则从0开始截取)。
  2. 如果 startIndex 大于或等于字符串的长度,则 slice() 返回一个空字符串。
  3. 如果 endIndex 省略,则将 slice() 字符提取到字符串的末尾。如果为负,它被视为 strLength + endIndex 其中strLength是字符串的长度。

          例: console.log('(-3, 9): ' + str.slice(-3, 9)); // '(-3, 9): io'

                  console.log('(-20, 2): ' + str.slice(-20, 2)); // '(-20, 2): qw'

                  console.log('(20, 2): ' + str.slice(20, 2)); // '(20, 2): '

                  console.log('(1): ' + str.slice(1)); // '(1): wertyuiop'

                  console.log('(0,-1): ' + str.slice(0,-1)); // '(0,-1): qwertyuio'

                   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值