數(shù)組字符串方法總結(jié)

.toUpperCase() 變成大寫

var a="string"
var b=a.toUpperCase()
console.log(b)//"STRING"

.toLowerCase() 變成小寫

var a="STRING"
var b=a.toLowerCase()
console.log(b)//"string"

.split() 以括號里面的內(nèi)容為分界點,讓字符串變?yōu)閿?shù)組

例子1:
var a="string-ing"
var b=a.split("-")
console.log(b)//["string","ing"]

例子2:
var a="string"
var b=a.split()//可以不寫東西
console.log(b)//["string"]

例子3
var a="string"
var b=a.split("")
console.log(b)//["s", "t", "r", "i", "n", "g"];

.join() 讓數(shù)組以括號里面的內(nèi)容合并成一個字符串

var a=[1,2,3,4,5,6]
var b=a.join("")
console.log(b)//123456

例子2:
var a=[1,2,3,4,5,6]
var b=a.join()
console.log(b)//1,2,3,4,5,6

substr() 獲取字符串指定的部分

var a="string"
var b=a.substr(1,2)//獲取了從1開始的后面2個字符串,包括1
////如果省略第二個參數(shù),會自動獲取到最后

console.log(b)//tr
console.log(a)//"string"  這個方法不會對原字符串做出任何修改

substring() 獲取字符串指定的部分

var a="string"
var b=a.substring(1,2)//獲取了從1開始的后面2個字符串,這個不包括1
//如果省略第二個參數(shù),會自動獲取到最后

console.log(b)//j
console.log(a)//"string"  這個方法不會對原字符串做出任何修改

reverse()把數(shù)組翻轉(zhuǎn)

var a=[1,2,3,4,5];
var b=a.reverse()
console.log(b)//[5,4,3,2,1]

toString//把數(shù)組變成字符串,就用逗號隔開

var a=[1,2,3,4,5,6]
var b=a.toString()
console.log(b)//1,2,3,4,5,6

splice() 切取數(shù)組的值(會改變原來的數(shù)組)

var a=[1,2,3,4,5,6]
var b=a.splice(0,1,2)//從0開始索引,竊取1位數(shù),會包括0,然后把竊取的位置添加2
console.log(b)//[1]
console.log(a)//[2,2,3,4,5,6]

slice()竊取數(shù)組的元素,不會改變原數(shù)組。

var a=[1,2,3,4,5,6]
var b=a.slice(0,3)包括第0位,從第0位開始竊取后面的三個元素。不會改變原數(shù)組
console.log(b)//[1,2,3]
console.log(a)//[1,2,3,4,5,6]

push() / pop() 從數(shù)組后面添加元素 / 從數(shù)組后面刪除元素 (都會改變原數(shù)組)

var a=[1,2,3,4,5,6]
a.push(7)
console.log(a)//[1,2,3,4,5,6,7]
a.pop()
console.log(a)//[1,2,3,4,5,6]

unshift() / shift() 從數(shù)組前端添加元素 / 從數(shù)組前端刪除元素 (都會改變原數(shù)組)

var a=[1,2,3,4,5,6]
a.unshift(7)
console.log(a)//[7,1,2,3,4,5,6]
a.shift() 
console.log(a)//[1,2,3,4,5,6]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容