【JavaScript】slice()、substring()、substr()的区别

【JavaScript】slice()、substring()、substr()的区别

马嘉楠    2008-12-16

 

共同学习,欢迎转载。转载请注明地址【http://blog.csdn.net/majianan/archive/2008/12/16/3528310.aspx】,谢谢O(∩_∩)O!

 

JavaScript中String 对象的slice()、substring()、substr()方法都能提取字符串的一部分,但使用时有所区别。

 

  • stringObject.slice(startIndex,endIndex)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。

1)参数 endIndex 可选,如果没有指定,则默认为字符串的长度 stringObject.length 。

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.slice(3)); // lo world!
  3.   alert(stringObject.slice(3,stringObject.length)); // lo world!

【注1】字符串中第一个字符的位置是从【0】开始的,最后一个字符的位置为【stringObject.length-1】,所以slice()方法返回的字符串不包括endIndex位置的字符。

2)startIndex 、endIndex 可以是负数。如果为负,则表示从字符串尾部开始算起。即-1表示字符串最后一个字符。

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.slice(-3)); // ld!
  3.   alert(stringObject.slice(-3,stringObject.length)); // ld!
  4.   alert(stringObject.slice(-3,-1)); // ld

【注2】合理运用负数可以简化代码

3)startIndex、endIndex 都是可选的,如果都不填则返回字符串 stringObject 的全部,等同于slice(0)

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.slice()); // hello world!
  3.   alert(stringObject.slice(0)); // hello world!

4)如果startIndex、endIndex 相等,则返回空串

【注3】String.slice() 与 Array.slice() 相似

 

 

  • stringObject.substring(startIndex、endIndex)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。

1)startIndex  是一个非负的整数,必须填写。endIndex 是一个非负整数,可选。如果没有,则默认为字符串的长度stringObject.length 。

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.substring(3)); // lo world!
  3.   alert(stringObject.substring(3,stringObject.length)); // lo world!
  4.   alert(stringObject.substring(3,7)); // lo w,空格也算在内[l][o][ ][w]

2)如果startIndex、endIndex 相等,则返回空串。如果startIndex 比 endIndex 大,则提取子串之前,调换两个参数。即stringObject.substring(startIndex,endIndex)等同于stringObject.substring(endIndex,startIndex)

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.substring(3,3)); // 空串
  3.   alert(stringObject.substring(3,7)); // lo w
  4.   alert(stringObject.substring(7,3)); // lo w

【注4】substring()相比,slice()更灵活,可以接收负参数。

 

 

  • stringObject.substr(startIndex,length)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )指定数目(length)的字符字符。

1)startIndex 必须填写,可以是负数。如果为负,则表示从字符串尾部开始算起。即-1表示字符串最后一个字符。

2)参数 length 可选,如果没有指定,则默认为字符串的长度 stringObject.length 。

  1.   var stringObject = "hello world!";
  2.   alert(stringObject.substr(3)); // lo world!
  3.   alert(stringObject.substr(3,stringObject.length)); // lo world!
  4.   alert(stringObject.substr(3,4)); // lo w

3)substr()可以代替slice()和substring()来使用,从上面代码看出 stringObject.substr(3,4) 等同于stringObject.substring(3,7)

【注5】ECMAscript 没有对该方法进行标准化,因此尽量少使用该方法。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值