zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

JavaScript 说明 区别 substr substring Slice splice
2023-06-13 09:14:25 时间

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

Anonnegativeintegerthatspecifiesthepositionwithinstringofthefirstcharacterofthedesiredsubstring.

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

to

Anonnegativeoptionalintegerthatisonegreaterthanthepositionofthelastcharacterofthedesiredsubstring.Ifthisargumentisomitted,thereturnedsubstringrunstotheendofthestring.

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



string.slice(start,end)

Arguments

start

Thestringindexwherethesliceistobegin.Ifnegative,thisargumentspecifiesapositionmeasuredfromtheendofthestring.Thatis,-1indicatesthelastcharacter,-2indicatesthesecondfromlastcharacter,andsoon.

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

end

Thestringindeximmediatelyaftertheendoftheslice.Ifnotspecified,thesliceincludesallcharactersfromstarttotheendofthestring.Ifthisargumentisnegative,itspecifiesapositionmeasuredfromtheendofthestring.

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

string.substr(start,length)

Arguments

start

Thestartpositionofthesubstring.Ifthisargumentisnegative,itspecifiesapositionmeasuredfromtheendofthestring:-1specifiesthelastcharacter,-2specifiesthesecond-to-lastcharacter,andsoon.

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

length

Thenumberofcharactersinthesubstring.Ifthisargumentisomitted,thereturnedsubstringincludesallcharactersfromthestartingpositiontotheendofthestring.

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



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

复制代码代码如下:

vars="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)