查詢是否包含某一字符
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() // 去空格