第3章 字符串的擴(kuò)展

查詢是否包含某一字符

let string = 'my name is tabBin'
string.indexOf(tabBin) // 11
string.includes('tabBin') // true
string.startWith('m') // true
string.endWith('m') // false, 應(yīng)該是n

repeat()

repeat方法返回一個(gè)新字符串,表示將原來(lái)的字符串重復(fù)幾次

let s = 'hello'
let new = s.repeat(2)
console.log(new) // hellohello

如果參數(shù)為小數(shù),會(huì)被取整;如果參數(shù)為負(fù)數(shù)或者infinity,會(huì)報(bào)錯(cuò)

let s = 'hello'
let new = s.repeat(2.7)
console.log(new) // hellohello

let new = s.repeat(-2) // 報(bào)錯(cuò)

模板字符串

模板字符串是增強(qiáng)版的字符串,用反引號(hào)(`)來(lái)表示。它可以當(dāng)做普通字符串使用,也可以用來(lái)定義多行字符串,或者在字符串中嵌入變量。

let [name, age, sex]  = [' tanBin ', ' 22 ', ' 男 '  ]
let string = "我的名字是`${name}`,年齡是${age},性別是${sex}"
console.log(string) // 我的名字是tanBin,年齡是22,性別是男

其他

let s = 'hello world'
s.replace('o', 'mmm') // hellmmm  world,字符串替換
s.split(' ') // ['hello', 'world'] 字符串分割,分后為一個(gè)數(shù)組
s.trim() // 去空格
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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