欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中substr,substring,slice.splice的區(qū)別說(shuō)明

 更新時(shí)間:2010年11月25日 22:17:29   作者:  
某些情況下,負(fù)數(shù)的參數(shù)不識(shí)別.所以盡量不要用負(fù)數(shù)作參數(shù).免得瀏覽器不兼容,造成程序的出錯(cuò).

substr() 方法可在字符串中抽取從 start 下標(biāo)開(kāi)始的指定數(shù)目的字符.

stringObject.substr(start,length);start必須,length可選.

start 是截取的開(kāi)始位置的下標(biāo),從0開(kāi)始算起,必須是數(shù)字.可以是負(fù)數(shù),-1是倒數(shù)第一個(gè)字符,-2是倒數(shù)第二個(gè)字符,以此類(lèi)推.

length 是要截取的字符的長(zhǎng)度,必須是數(shù)字.如果未指定,則從start位置處開(kāi)始截取到字符串結(jié)尾.

substr 指定的是字符串的開(kāi)始下標(biāo)跟截取長(zhǎng)度,所以可以替代substring跟slice使用.

重要事項(xiàng):ECMAscript 沒(méi)有對(duì)該方法進(jìn)行標(biāo)準(zhǔn)化,因此反對(duì)使用它。

substring() 方法用于提取字符串中介于兩個(gè)指定下標(biāo)之間的字符。

stringObject.substring(start,end);start必須,end可選.

start 是截取的開(kāi)始位置的下標(biāo),從0開(kāi)始算起,必須是非負(fù)數(shù)字.(w3c說(shuō)必須是非負(fù)整數(shù),試驗(yàn)了下,不是整數(shù)也可以.)

end 必須是數(shù)字.如果未指定,則從start位置處開(kāi)始截取到字符串結(jié)尾.

注意事項(xiàng):substring截取的字符不包括end處的字符.所以截取的長(zhǎng)度為end-start.

               start=end的話(huà),返回空字符串,長(zhǎng)度為0.

重要事項(xiàng):substring不接收負(fù)的參數(shù),slice和substr可以.

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

stringObject.slice(start,end);start必須,end可選.

start 要抽取的片斷的起始下標(biāo)。如果是負(fù)數(shù),則該參數(shù)規(guī)定的是從字符串的尾部開(kāi)始算起的位置。也就是說(shuō),-1 指字符串的最后一個(gè)字符,-2 指倒數(shù)第二個(gè)字符,以此類(lèi)推。

end 緊接著要抽取的片段的結(jié)尾的下標(biāo)。若未指定此參數(shù),則要提取的子串包括 start 到原字符串結(jié)尾的字符串。如果該參數(shù)是負(fù)數(shù),那么它規(guī)定的是從字符串的尾部開(kāi)始算起的位置。

splice() 方法用于插入、刪除或替換數(shù)組的元素。

arrayObject.splice(index,howmany,element1,.....,elementX)index,howmany必須,其他可選.

index 規(guī)定從何處添加/刪除元素。該參數(shù)是開(kāi)始插入和(或)刪除的數(shù)組元素的下標(biāo),必須是數(shù)字。

 

howmany 規(guī)定應(yīng)該刪除多少元素。必須是數(shù)字,但可以是 "0"。如果未規(guī)定此參數(shù),則刪除從 index 開(kāi)始到原數(shù)組結(jié)尾的所有元素。

element1 規(guī)定要添加到數(shù)組的新元素。從 index 所指的下標(biāo)處開(kāi)始插入。

elementx 可向數(shù)組添加若干元素。

注釋?zhuān)?/SPAN>請(qǐng)注意,splice() 方法與 slice() 方法的作用是不同的,splice() 方法會(huì)直接對(duì)數(shù)組進(jìn)行修改。

所有提示:某些情況下,負(fù)數(shù)的參數(shù)不識(shí)別.所以盡量不要用負(fù)數(shù)作參數(shù).免得瀏覽器不兼容,造成程序的出錯(cuò).


 

這是JavaScript 權(quán)威指南上的說(shuō)明,象我這種E文很爛的就能勉強(qiáng)看懂一下,并沒(méi)有對(duì)著翻譯,只是按照理解說(shuō)明了下。


string.substring(from, to)

Arguments

from

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

       指定想要得到字符串的開(kāi)始位置,即索引(非負(fù)整數(shù))

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.

       指定想要得到字符串的結(jié)束位置,不包括該位置的字符(非負(fù)整數(shù),可選,沒(méi)有指定則返回從指定開(kāi)始位置到原字符串結(jié)束位置)




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.

        指定想要得到字符串的開(kāi)始的位置,即索引。

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.

        指定想要得到字符串的結(jié)束位置,不包括該位置的字符(可選,沒(méi)有指定則返回從指定開(kāi)始位置到原字符串結(jié)束位置)


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.

        指定想要得到字符串的開(kāi)始的位置,即索引。

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.

        指定想要得到字符串的長(zhǎng)度,(可選,沒(méi)有指定則返回從指定開(kāi)始位置到原字符串結(jié)束位置)




PS:這三個(gè)方法均返回截取原字符串的一部分新字符串,第2個(gè)參數(shù)均為可選參數(shù),并且其實(shí)所有的參數(shù)均可以為負(fù)整數(shù)。

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

這兩個(gè)方法差不多,都是指定開(kāi)始和結(jié)束位置返回新字符串,在參數(shù)均為正整數(shù)的時(shí)候返回結(jié)果一樣,當(dāng)參數(shù)為負(fù)整數(shù)的時(shí)候,string.substring(from, to) 把負(fù)整數(shù)都當(dāng)作0處理,而 string.slice(start, end) 將把負(fù)整數(shù)加上該字符串的長(zhǎng)度處理。

string.substr(start, length)

這個(gè)方法只在第二個(gè)參數(shù)上指定的是新字符串的長(zhǎng)度,對(duì)于負(fù)正數(shù)和string.slice(start, end)處理一樣,把負(fù)整數(shù)加上原字符串的長(zhǎng)度。
Example

復(fù)制代碼 代碼如下:

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" 實(shí)際上是 s.substring(0,2) 較小的參數(shù)會(huì)在前面
s.slice(2,-3) // Returns "cd" 實(shí)際上是 s.slice(2,4)
s.substr(2,-3) // Returns "cdef" 實(shí)際上是 s.slice(2,4)

相關(guān)文章

最新評(píng)論