//字符串的扩展 //字符串遍历器 for (let codePoint of 'string') { console.log(codePoint) // s t r i n g } for (let v of 'string') { console.log(v) // 0~5 } // includes():返回布尔值,表示是否找到了参数字符串。 // startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。 // endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。 let str = 'word hello'; console.log( str.includes('h'), // true str.startsWith('word'), // true str.endsWith('o') // true ); // repeat() 返回一个新字符串,表示将原字符串重复n次。 console.log( // 将abc重复4次 'abc'.repeat(4) // abcabcabcabc );
//padStart() padEnd() 字符串不够指定长度,会在头部或尾部补全。 // 参数一:指定字符串的最小长度 // 参数二:用来补全的字符串 console.log( 'x'.padStart(3, 'ab'), //abax 'x'.padEnd(3, 'ab') //xab );