ES6中字串擴充套件

2022-09-05 02:06:06 字數 1040 閱讀 7529

① for...of 遍歷字串:

例如:

1

for(let codepoint of 'string')

執行結果:

說明:三個方法都接收兩個引數,第乙個引數為檢索的值,第二個引數為檢索的起始位置,返回布林值

例如:

1 let s = 'hello world!';

23 const [a, b, c] =[

4 s.startswith('hello', 2),

5 s.endswith('!'),

6 s.includes('o w')7];

89 console.log(a, b, c); //

false true true

③  repeat()

說明:repeat 方法返回乙個新字串,表示將原字串重複 n 次。

例如:

1 'str'.repeat('3') //

'strstrstr'

④ padstart(), padend()

說明:接受兩個引數,第乙個引數為字串補全生效的最大長度,第二個引數為補全的字串。

常見用途:補全指定位數,提示字串格式。

例如:

1 '123456'.padstart(10, '0') //

"0000123456"

2 '09-12'.padstart(10, 'yyyy-mm-dd') //

"yyyy-09-12"

⑤  模版字串(``)

例如:

1 const str = 'world';

2 const template =`hello $`;

3 console.log(template); //

hello world

ES6中字串的擴充套件

在es5中,可以使用indexof方法和lastindexof方法查詢字串 let str hello world alert str.indexof o 4alert str.lastindexof o 7alert str.lastindexof z 1es6中,又新增了3個方法用於特定字元的查...

ES6中for迴圈 函式擴充套件 字串擴充套件 建立類

1.for var arr a b c console.log for for var i 0 i arr.length i 2.foreach var arr a b c console.log console.log foreach 遍歷 陣列 引數一 當前元素 引數二 當前元素的索引值 arr...

ES6 字串擴充套件

1 字串可以使用 u x的形式來表達乙個字元,x叫做字元的碼點,x的範圍是0000 ffff,超過ffff的碼點需要用兩個雙位元組表示 如果我們 u後面的16進製制的值大於ffff,我們需要加乙個大括號 u讓js正確解析。2 我們應該都了解,漢字一般都需要兩個雙位元組來表示,在js中兩個位元組佔乙個...