javascript中substr,substring,slice.splice的区别说明


Posted in Javascript onNovember 25, 2010

substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符.

stringObject.substr(start,length);start必须,length可选.

start 是截取的开始位置的下标,从0开始算起,必须是数字.可以是负数,-1是倒数第一个字符,-2是倒数第二个字符,以此类推.

length 是要截取的字符的长度,必须是数字.如果未指定,则从start位置处开始截取到字符串结尾.

substr 指定的是字符串的开始下标跟截取长度,所以可以替代substring跟slice使用.

重要事项:ECMAscript 没有对该方法进行标准化,因此反对使用它。

substring() 方法用于提取字符串中介于两个指定下标之间的字符。

stringObject.substring(start,end);start必须,end可选.

start 是截取的开始位置的下标,从0开始算起,必须是非负数字.(w3c说必须是非负整数,试验了下,不是整数也可以.)

end 必须是数字.如果未指定,则从start位置处开始截取到字符串结尾.

注意事项:substring截取的字符不包括end处的字符.所以截取的长度为end-start.

               start=end的话,返回空字符串,长度为0.

重要事项:substring不接收负的参数,slice和substr可以.

slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。

stringObject.slice(start,end);start必须,end可选.

start 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。

end 紧接着要抽取的片段的结尾的下标。若未指定此参数,则要提取的子串包括 start 到原字符串结尾的字符串。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。

splice() 方法用于插入、删除或替换数组的元素。

arrayObject.splice(index,howmany,element1,.....,elementX)index,howmany必须,其他可选.

index 规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。

 

howmany 规定应该删除多少元素。必须是数字,但可以是 "0"。如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。

element1 规定要添加到数组的新元素。从 index 所指的下标处开始插入。

elementx 可向数组添加若干元素。

注释:请注意,splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改。

所有提示:某些情况下,负数的参数不识别.所以尽量不要用负数作参数.免得浏览器不兼容,造成程序的出错.

 

这是JavaScript 权威指南上的说明,象我这种E文很烂的就能勉强看懂一下,并没有对着翻译,只是按照理解说明了下。


string.substring(from, to)

Arguments

from

A nonnegative integer that specifies the position within string of the first character of the desired substring.

       指定想要得到字符串的开始位置,即索引(非负整数)

to

A nonnegative optional integer that is one greater than the position of the last character of the desired substring. If this argument is omitted, the returned substring runs to the end of the string.

       指定想要得到字符串的结束位置,不包括该位置的字符(非负整数,可选,没有指定则返回从指定开始位置到原字符串结束位置)


string.slice(start, end)

Arguments

start

The string index where the slice is to begin. If negative, this argument specifies a position measured from the end of the string. That is, -1 indicates the last character, -2 indicates the second from last character, and so on.

        指定想要得到字符串的开始的位置,即索引。

end

The string index immediately after the end of the slice. If not specified, the slice includes all characters from start to the end of the string. If this argument is negative, it specifies a position measured from the end of the string.

        指定想要得到字符串的结束位置,不包括该位置的字符(可选,没有指定则返回从指定开始位置到原字符串结束位置)


string.substr(start, length)

Arguments

start

The start position of the substring. If this argument is negative, it specifies a position measured from the end of the string: -1 specifies the last character, -2 specifies the second-to-last character, and so on.

        指定想要得到字符串的开始的位置,即索引。

length

The number of characters in the substring. If this argument is omitted, the returned substring includes all characters from the starting position to the end of the string.

        指定想要得到字符串的长度,(可选,没有指定则返回从指定开始位置到原字符串结束位置)


PS:这三个方法均返回截取原字符串的一部分新字符串,第2个参数均为可选参数,并且其实所有的参数均可以为负整数。

string.substring(from, to)
string.slice(start, end)

这两个方法差不多,都是指定开始和结束位置返回新字符串,在参数均为正整数的时候返回结果一样,当参数为负整数的时候,string.substring(from, to) 把负整数都当作0处理,而 string.slice(start, end) 将把负整数加上该字符串的长度处理。

string.substr(start, length)

这个方法只在第二个参数上指定的是新字符串的长度,对于负正数和string.slice(start, end)处理一样,把负整数加上原字符串的长度。
Example

var s = "abcdefg"; s.substring(1,4) // Returns "bcd" 
s.slice(1,4) // Returns "bcd" 
s.substr(1,4) // Returns "bcde" 
s.substring(2,-3) // Returns "ab" 实际上是 s.substring(0,2) 较小的参数会在前面 
s.slice(2,-3) // Returns "cd" 实际上是 s.slice(2,4) 
s.substr(2,-3) // Returns "cdef" 实际上是 s.slice(2,4)
Javascript 相关文章推荐
在IE下获取object(ActiveX)的Param的代码
Sep 15 Javascript
jWiard 基于JQuery的强大的向导控件介绍
Oct 28 Javascript
js定时器(执行一次、重复执行)
Mar 07 Javascript
利用函数的惰性载入提高javascript代码执行效率
May 05 Javascript
Javascript实现的简单右键菜单类
Sep 23 Javascript
javascript 用函数实现继承详解
May 28 Javascript
js简单判断flash是否加载完成的方法
Jun 21 Javascript
JavaScript字符串对象
Jan 14 Javascript
JS实现的判断方法、变量是否存在功能示例
Mar 28 Javascript
layer.open关闭父窗口 以及调用父页面的方法
Aug 17 Javascript
vue设置一开始进入的页面教程
Oct 28 Javascript
JavaScript实现PC端横向轮播图
Feb 07 Javascript
JavaScript中的类继承
Nov 25 #Javascript
js getBoundingClientRect() 来获取页面元素的位置
Nov 25 #Javascript
腾讯的ip接口 方便获取当前用户的ip地理位置
Nov 25 #Javascript
js删除所有的cookie的代码
Nov 25 #Javascript
javascript 广告后加载,加载完页面再加载广告
Nov 25 #Javascript
js页面跳转常用的几种方式
Nov 25 #Javascript
简略的前端架构心得&&基于editor为例子的编码小技巧
Nov 25 #Javascript
You might like
PHP 根据IP地址控制访问的代码
2010/04/22 PHP
PHP随机生成随机个数的字母组合示例
2014/01/14 PHP
PHP判断浏览器、判断语言代码分享
2015/03/05 PHP
PHP微信刮刮卡 附微信接口
2016/07/22 PHP
JavaScript 判断判断某个对象是Object还是一个Array
2010/01/28 Javascript
Js 弹出框口并返回值的两种常用方法
2010/12/30 Javascript
特殊情况下如何获取span里面的值
2014/05/20 Javascript
教你使用javascript简单写一个页面模板引擎
2015/05/05 Javascript
Jquery Mobile 自定义按钮图标
2015/11/18 Javascript
大型JavaScript应用程序架构设计模式
2016/06/29 Javascript
Angular ng-repeat 对象和数组遍历实例
2016/09/14 Javascript
javascript 中设置window.location.href跳转无效问题解决办法
2017/02/09 Javascript
vue bus全局事件中心简单Demo详解
2018/02/26 Javascript
angularJs 表格添加删除修改查询方法
2018/02/27 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
vue-router之nuxt动态路由设置的两种方法小结
2018/09/26 Javascript
node(koa2) web应用模块介绍详解
2019/03/29 Javascript
使用Python保存网页上的图片或者保存页面为截图
2016/03/05 Python
python解析基于xml格式的日志文件
2017/02/25 Python
Python基于numpy灵活定义神经网络结构的方法
2017/08/19 Python
python创建属于自己的单词词库 便于背单词
2019/07/30 Python
关于Python字符串显示u...的解决方式
2020/03/06 Python
浅谈keras保存模型中的save()和save_weights()区别
2020/05/21 Python
PyTorch预训练Bert模型的示例
2020/11/17 Python
前端canvas动画如何转成mp4视频的方法
2019/06/17 HTML / CSS
世界上最大的街头服饰网站:Karmaloop
2017/02/04 全球购物
《两个铁球同时着地》教学反思
2014/02/13 职场文书
如何写一份好的英文求职信
2014/03/19 职场文书
报关报检委托书
2014/04/08 职场文书
《三个小伙伴》教学反思
2014/04/11 职场文书
银行开户授权委托书格式
2014/10/10 职场文书
2015年小学教导处工作总结
2015/05/26 职场文书
百年校庆感言
2015/08/01 职场文书
如何让vue长列表快速加载
2021/03/29 Vue.js
nginx对http请求处理的各个阶段详析
2021/03/31 Servers
java设计模式--七大原则详解
2021/07/21 Java/Android