前言
本章介紹字符串對象的新增方法戚啥。不常用的方法不做重點(diǎn)筆記猫十。
本章原文鏈接:字符串的新增方法
includes()拖云、startsWith()乏苦、endsWith()
確定一個(gè)字符串是否包含在另一個(gè)字符串中汇荐。ES6 又提供了三種新方法掀淘。
- includes() 方法用于判斷一個(gè)字符串是否包含在另一個(gè)字符串中
- startsWith() 方法用來判斷當(dāng)前字符串是否以另外一個(gè)給定的子字符串開頭
- endsWith() 方法用來判斷當(dāng)前字符串是否是以另外一個(gè)給定的子字符串“結(jié)尾”的
方法名 | 返回值 | 描述 | 第二個(gè)參數(shù) : Num |
---|---|---|---|
includes() | 布爾值 | 一個(gè)字符串是否包含參數(shù)字符串 | 從第 Num 個(gè)位置直到字符串結(jié)束 |
startsWith() | 布爾值 | 一個(gè)字符串的開頭是否包含參數(shù)字符串 | 從第 Num 個(gè)位置直到字符串結(jié)束 |
endsWith() | 布爾值 | 一個(gè)字符串的結(jié)尾是否包含參數(shù)字符串 | 表示前 Num 個(gè)字符 |
let sampleString = 'Hello world!';
const sample1 = sampleString.includes('llo');
const sample2 = sampleString.startsWith('H');
const sample3 = sampleString.endsWith('d!');
console.log(sample1, sample2, sample3); // true true true
// 使用第二個(gè)參數(shù)
const sample11 = sampleString.includes('llo', 1);
const sample12 = sampleString.startsWith('H', 1);
const sample13 = sampleString.endsWith('d!', 10);
console.log(sample11, sample12, sample13); // true false false
repeat()
repeat()
方法返回一個(gè)新字符串冕碟,表示將原字符串重復(fù)n
次安寺。
參數(shù)為小數(shù)就會(huì)向下取整叹放,為負(fù)數(shù)和無窮(Infinity
)則會(huì)報(bào)錯(cuò).
參數(shù) | 處理 |
---|---|
小數(shù) | 向下取整 |
字符串 | 先轉(zhuǎn)換成數(shù)字 |
負(fù)數(shù) | 報(bào)錯(cuò) |
Infinity | 報(bào)錯(cuò) |
const SAMPLE = 'Ha';
let sample1 = SAMPLE.repeat(4); // 重復(fù)四次
console.log(sample1); // HaHaHaHa
let sample2 = SAMPLE.repeat(1.8); // 參數(shù)為小書井仰,向下取整
console.log(sample2); // Ha
let sample3 = SAMPLE.repeat('3'); // 參數(shù)為字符串
console.log(sample3); // HaHaHa
let sample4 = SAMPLE.repeat(-4); // 參數(shù)為負(fù)數(shù)
console.log(sample4); // 直接報(bào)錯(cuò) Invalid count value
padStart(),padEnd()
ES2017 引入了字符串補(bǔ)全長度的功能合是。如果某個(gè)字符串不夠指定長度聪全,會(huì)在頭部或尾部補(bǔ)全娃圆。
-
padStart()
用于頭部補(bǔ)全讼呢。 -
padEnd()
用于尾部補(bǔ)全悦屏。
padStart()
與padEnd()
都接受兩個(gè)參數(shù)
- 參數(shù)一:字符串補(bǔ)全生效的最大長度。如果當(dāng)前字符串小于指定的長度幕帆,則進(jìn)行補(bǔ)全失乾。
- 參數(shù)二:用來補(bǔ)全的字符串。如果字符串長度過長纽竣,則會(huì)刪除后面的多出的字符串蜓氨。默認(rèn)空格補(bǔ)全。
const SAMPLE = '368';
let newSample = SAMPLE.padStart(11, "000,");
let newSample1 = SAMPLE.padEnd(11, ",000");
console.log(newSample); // 000,000,368
console.log(newSample1); // 368,000,000
trimStart()港令,trimEnd()
ES2019 對字符串實(shí)例新增了trimStart()
和trimEnd()
這兩個(gè)方法。去處字符串的空白符淋淀,它們的行為與trim()
一致绅喉,空白符包括:空格、制表符 tab革屠、換行符等其他空白符等似芝。
trim()
消除字符串的頭尾空白符。
trimStart()
消除字符串頭部的空白符寞奸,別名為:trimLeft()
;
trimEnd()
消除字符串尾部的空白符。別名為:trimRight()
;
返回值為一個(gè)新字符串瓷翻,表示除去空格的調(diào)用字符串齐帚。
const SAMPLE = ' Ha ';
let newSample = SAMPLE.trimStart();
let newSample1 = SAMPLE.trimEnd();
console.log(newSample); // Ha
console.log(newSample1); // Ha
matchAll()
matchAll()
方法返回一個(gè)包含所有匹配正則表達(dá)式的結(jié)果,返回的是一個(gè)迭代器(Iterator)。
這塊不深入饥伊,后面正則表達(dá)式擴(kuò)展再進(jìn)行詳解。
const string = 'sample1sample2sample3';
const regex = /sample/g;
for (const match of string.matchAll(regex)) {
console.log(match);
}
// 遍歷輸出
/*
['sample', index: 0, input: 'sample1sample2sample3', groups: undefined]
['sample', index: 7, input: 'sample1sample2sample3', groups: undefined]
['sample', index: 14, input: 'sample1sample2sample3', groups: undefined]
*/
replaceAll()
簡單來說,可以一次性替換所有匹配冻押。
// 語法
String.prototype.replaceAll(searchValue, replacement);
replaceAll()
方法返回一個(gè)新字符串括袒,新字符串所有滿足 pattern
的部分都已被replacement
替換
兩個(gè)參數(shù)
- 參數(shù)一: 可以是一個(gè)字符串锹锰,也可以是一個(gè)全局的正則表達(dá)式(帶有g(shù)修飾符)。
- 參數(shù)二:第二個(gè)參數(shù)
replacement
是一個(gè)字符串痢士,表示替換的文本怠蹂,其中可以使用一些特殊字符串。除了為字符串赞庶,也可以是一個(gè)函數(shù),該函數(shù)的返回值將替換掉第一個(gè)參數(shù)searchValue
匹配的文本摊册。-
$&
:匹配的字符串。 -
$
`:匹配結(jié)果前面的文本棋枕。 -
$'
:匹配結(jié)果后面的文本白修。 -
$n
:匹配成功的第n組內(nèi)容,n是從1開始的自然數(shù)重斑。這個(gè)參數(shù)生效的前提是兵睛,第一個(gè)參數(shù)必須是正則表達(dá)式。 -
$$
:指代美元符號$。
-
const SAMPLE = 'HaHa祖很,huhu,hehe';
let sample1 = SAMPLE.replaceAll('h', 'H');
let sample2 = SAMPLE.replaceAll('H', () => 'h');
console.log(sample1); // HaHa假颇,HuHu胚鸯,HeHe
console.log(sample2); // haha,huhu拆融,hehe
String.raw()
ES6 還為原生的 String
對象蠢琳,提供了一個(gè)raw()
方法啊终。是用來獲取一個(gè)模板字符串的原始字符串的镜豹,在大多數(shù)情況下, String.raw()
是用來處理模版字符串的。
console.log(`Hi\n${2+3}!`); //
/*
輸出
Hi
5!
*/
console.log( String.raw`Hi\n${2+3}!`); // 輸出 Hi\n5!
String.fromCodePoint()
String.fromCodePoint()
靜態(tài)方法返回使用指定的代碼點(diǎn)序列創(chuàng)建的字符串蓝牲√酥可以識(shí)別大于0xFFFF
的字符,彌補(bǔ)了String.fromCharCode()
方法的不足例衍。
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804));
// 直接輸出 ?★?你