javascript中substr,substring,slice.splice的區(qū)別說明
substr() 方法可在字符串中抽取從 start 下標(biāo)開始的指定數(shù)目的字符.
stringObject.substr(start,length);start必須,length可選.
start 是截取的開始位置的下標(biāo),從0開始算起,必須是數(shù)字.可以是負數(shù),-1是倒數(shù)第一個字符,-2是倒數(shù)第二個字符,以此類推.
length 是要截取的字符的長度,必須是數(shù)字.如果未指定,則從start位置處開始截取到字符串結(jié)尾.
substr 指定的是字符串的開始下標(biāo)跟截取長度,所以可以替代substring跟slice使用.
重要事項:ECMAscript 沒有對該方法進行標(biāo)準(zhǔn)化,因此反對使用它。
substring() 方法用于提取字符串中介于兩個指定下標(biāo)之間的字符。
stringObject.substring(start,end);start必須,end可選.
start 是截取的開始位置的下標(biāo),從0開始算起,必須是非負數(shù)字.(w3c說必須是非負整數(shù),試驗了下,不是整數(shù)也可以.)
end 必須是數(shù)字.如果未指定,則從start位置處開始截取到字符串結(jié)尾.
注意事項:substring截取的字符不包括end處的字符.所以截取的長度為end-start.
start=end的話,返回空字符串,長度為0.
重要事項:substring不接收負的參數(shù),slice和substr可以.
slice() 方法可提取字符串的某個部分,并以新的字符串返回被提取的部分。
stringObject.slice(start,end);start必須,end可選.
start 要抽取的片斷的起始下標(biāo)。如果是負數(shù),則該參數(shù)規(guī)定的是從字符串的尾部開始算起的位置。也就是說,-1 指字符串的最后一個字符,-2 指倒數(shù)第二個字符,以此類推。
end 緊接著要抽取的片段的結(jié)尾的下標(biāo)。若未指定此參數(shù),則要提取的子串包括 start 到原字符串結(jié)尾的字符串。如果該參數(shù)是負數(shù),那么它規(guī)定的是從字符串的尾部開始算起的位置。
splice() 方法用于插入、刪除或替換數(shù)組的元素。
arrayObject.splice(index,howmany,element1,.....,elementX)index,howmany必須,其他可選.
index 規(guī)定從何處添加/刪除元素。該參數(shù)是開始插入和(或)刪除的數(shù)組元素的下標(biāo),必須是數(shù)字。
howmany 規(guī)定應(yīng)該刪除多少元素。必須是數(shù)字,但可以是 "0"。如果未規(guī)定此參數(shù),則刪除從 index 開始到原數(shù)組結(jié)尾的所有元素。
element1 規(guī)定要添加到數(shù)組的新元素。從 index 所指的下標(biāo)處開始插入。
elementx 可向數(shù)組添加若干元素。
注釋:請注意,splice() 方法與 slice() 方法的作用是不同的,splice() 方法會直接對數(shù)組進行修改。
所有提示:某些情況下,負數(shù)的參數(shù)不識別.所以盡量不要用負數(shù)作參數(shù).免得瀏覽器不兼容,造成程序的出錯.
這是JavaScript 權(quán)威指南上的說明,象我這種E文很爛的就能勉強看懂一下,并沒有對著翻譯,只是按照理解說明了下。
Arguments
-
from
-
A nonnegative integer that specifies the position within string of the first character of the desired substring.
- 指定想要得到字符串的開始位置,即索引(非負整數(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é)束位置,不包括該位置的字符(非負整數(shù),可選,沒有指定則返回從指定開始位置到原字符串結(jié)束位置)
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.
- 指定想要得到字符串的結(jié)束位置,不包括該位置的字符(可選,沒有指定則返回從指定開始位置到原字符串結(jié)束位置)
string.substr(start, length)
-
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.
- 指定想要得到字符串的長度,(可選,沒有指定則返回從指定開始位置到原字符串結(jié)束位置)
PS:這三個方法均返回截取原字符串的一部分新字符串,第2個參數(shù)均為可選參數(shù),并且其實所有的參數(shù)均可以為負整數(shù)。
string.substring(from, to)
string.slice(start, end)
這兩個方法差不多,都是指定開始和結(jié)束位置返回新字符串,在參數(shù)均為正整數(shù)的時候返回結(jié)果一樣,當(dāng)參數(shù)為負整數(shù)的時候,string.substring(from, to) 把負整數(shù)都當(dāng)作0處理,而 string.slice(start, end) 將把負整數(shù)加上該字符串的長度處理。
string.substr(start, length)
這個方法只在第二個參數(shù)上指定的是新字符串的長度,對于負正數(shù)和string.slice(start, end)處理一樣,把負整數(shù)加上原字符串的長度。
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) 較小的參數(shù)會在前面
s.slice(2,-3) // Returns "cd" 實際上是 s.slice(2,4)
s.substr(2,-3) // Returns "cdef" 實際上是 s.slice(2,4)
相關(guān)文章
JavaScript學(xué)習(xí)筆記(十七)js 優(yōu)化
在JavaScript中,我們可以使用for(;;),while(),for(in)三種循環(huán),事實上,這三種循環(huán)中for(in)的效率極差,因為他需要查詢散列鍵,只要可以就應(yīng)該盡量少用。2010-02-02用Object.prototype.toString.call(obj)檢測對象類型原因分析
在本篇文章里我們給大家剖析了用Object.prototype.toString.call(obj)檢測對象類型的原因,需要的朋友們可以學(xué)習(xí)下。2018-10-10JavaScript入門教程(9) Document文檔對象
Document文檔對象是JavaScript中window和frames對象的一個屬性,是顯示于窗口或框架內(nèi)的一個文檔。2009-01-01表單的焦點順序tabindex和對應(yīng)enter鍵提交
表單的焦點tab鍵順序:tabindex決定,如果值缺省,擇按順序一一對應(yīng),想詳細了解此功能應(yīng)用的朋友可以參考下2013-01-01在JavaScript中操作時間之getMonth()方法的使用
這篇文章主要介紹了在JavaScript中操作時間之getMonth()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06