· 字符串
創(chuàng)建
字符串一般直接創(chuàng)建:
let str = 'abvkl'
也可以通過new來創(chuàng)建(不推薦):
let str = new String("abv")
因為通過new創(chuàng)建的是對象,性能不好.而且相同的字符串不能用===來判斷,因為它們是不同的對象.
字符串方法和屬性
注意: 字符串的方法都是會返回新的字符串,并不會改變原數(shù)據(jù)!!!
1.查找
length 返回字符串長度
indexOf() 返回指定字符串在文本中首次出現(xiàn)的索引, 如果沒有則返回-1 范圍: 第二個參數(shù)到最后
lastIndexOf() 返回的是最后一次出現(xiàn)的索引,如果沒有返回-1 范圍: 0到第二個參數(shù)
includes() 指定字符串是否包含在某個字符串里,返回true/false(indexOf()返回的是索引值/-1)
startsWith() 是否以某個字符開頭,第二個參數(shù)表示開始位置,省略則默認(rèn)為0
endsWith() 是否以某個字符結(jié)束,第二個參數(shù)表示結(jié)束位置,省略則默認(rèn)最后一位
let str = 'this is a test'
console.log(str.indexOf('is')) // 2
console.log(str.lastIndexOf('is')) // 5
console.log(str.indexOf('is', 11)) // -1(因為是從第11位開始找的,所以沒找到,返回-1)
console.log(str.lastIndexOf('is', 11)) // 5(0到11之間找)
console.log(str.includes('q')) // false
console.log(str.startsWith('a', 2)) // false
console.log(str.startsWith('i', 2)) // true
console.log(str.endsWith('i', 4)) // false
console.log(str.endsWith('i', 3)) // true
search() 搜索特定字符串,返回位置
區(qū)別:
search()可以用正則表達式,indexOf()不能;
search()不能設(shè)置第二個參數(shù),indexOf()可以.
match() 返回字符串或者正則表達式的匹配
matchAll() 返回一個正則表達式在當(dāng)前字符串的所有匹配
let str1 = "1 plus 1 equals 2"
console.log(str1.match("s")) // ["s", index: 5, input: "1 plus 1 equals 2", groups: undefined]
console.log(str1.match(/\d+/g)) // ["1", "1", "2"]
console.log([...str1.matchAll(/\d+/g)]) // [Array(1), Array(1), Array(1)](具體見下圖)
2.截取
slice(start, end) 返回start到end的部分(包括start,不包括end)
如果參數(shù)是負(fù)數(shù),則從倒數(shù)開始
如果只有一個參數(shù),則截取從這個參數(shù)到末尾
let str = 'this is a test'
console.log(str.slice(5, 7)) // is
console.log(str.slice(-5, -8)) //
console.log(str.slice(-8, -5)) // s a
console.log(str.slice(5)) // is a test
console.log(str.slice(-4)) // test
substring(start, end) 和slice類似,參數(shù)不能接受負(fù)值
let str = 'this is a test'
console.log(str.substring(5, 7)) // is
console.log(str.substring(5)) // is a test
substr(start, length) 第一個參數(shù)表示開始位置,第二個參數(shù)是截取長度
第一個參數(shù)可以為負(fù)
如果省略第二個參數(shù)則到結(jié)尾
let str = 'this is a test'
console.log(str.substr(5, 7)) // is a te
console.log(str.substr(5)) // is a test
console.log(str.substr(-5)) // test
3.字符串去空格
trim() 去掉兩頭空格
trimStart()/trimLeft() 去掉左邊空格
trimEnd()/trimRight() 去掉右邊空格
let mystr = " 1234a "
console.log(mystr.trim()) // 1234a
console.log(mystr.trimLeft()) // 1234a
console.log(mystr.trimStart()) // 1234a
console.log(mystr.trimRight()) // 1234a
console.log(mystr.trimEnd()) // 1234a
4.字符串替換
replace() 用第二個參數(shù)替換第一個參數(shù)(只替換找到的第一個)
可以用正則表達式
正則表達式用/XXX/,不是引號
正則的i表示不區(qū)分大小寫,g表示全局查找(會替換所有的)
let str = 'this is Is a test'
console.log(str.replace("is", "at")) // that is Is a test
console.log(str.replace(/is/i, "at")) // that is Is a test
console.log(str.replace(/is/g, "at")) // that at Is a test
console.log(str.replace(/is/gi, "at")) // that at at a test
5.提取字符串字符
charAt() 返回指定下標(biāo)的字符
charCodeAt() 返回指定下標(biāo)字符的unicode 編碼
也可以用屬性訪問(不推薦): str[index]
let str = 'this is Is a test'
console.log(str.charAt(1)) // h
console.log(str.charCodeAt(1)) // 104
console.log(str[1]) // h
6.連接
concat() 連接兩個字符串
相當(dāng)于+
let str = 'this is Is a test'
console.log(str.concat("Wedesday", "wrong")) // this is a testWedesdaywrong
console.log(str + "Wedesday" + "wrong") // this is a testWedesdaywrong
concat()方法也不會改變原字符串!
7.大小寫轉(zhuǎn)換
toUpperCase() 轉(zhuǎn)為大寫
toLowerCase 轉(zhuǎn)為小寫
8.字符串轉(zhuǎn)為數(shù)組
split() 將字符串切割成數(shù)組
let str2 = "hello"
console.log(str2.split()) // ["hello"]
console.log(str2.split("")) // ["h", "e", "l", "l", "o"]
9.字符串重復(fù)
repeat() 表示重復(fù)幾次
(參數(shù)如果是小數(shù),會被取整,如果是0到-1之間的小數(shù),會被視為0,是其他負(fù)數(shù)則報錯,如果是字符串,則會先被轉(zhuǎn)成數(shù)字類型)
const str="safijjkc";
console.log(str.repeat(3))//safijjkcsafijjkcsafijjkc
10.長度補全
padSrart() padEnd() 如果長度不夠,則會在頭部或者尾部自動補全(第一個參數(shù)表示位數(shù))
省略第二個參數(shù),則會用空格補全
console.log('ww'.padStart(5,'ab'));//abaww
console.log('ww'.padEnd(5,'ab'));//wwaba
console.log('ww'.padStart(5));// ww