字符串的新增方法
1.includes(),startsWith(),endsWith()
includes():返回布尔值,表示是否找到了参数字符串。
startsWith():返回布尔值,表示参数字符串是否在原字符串的头部
endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部
//传统上,javasrcipt 只有indexOf 方法,可以来确定一个字符串是否包含在另一个字符串中。ES6又提供了三种新方法。后面加个 参数表示开始搜索的位置 startsWith() 从第n=0开始算; endsWith()从前几个字符 n=1
let hello="Hello world!"'
console.log(hello);
console.log(hello.includes("a"));
console.log(hello.startsWith("w",6));
console.log(hello.endsWith("!"));
console.log(hello.endsWith("w",7));
2. repeat()
repeat()返回一个新字符串,表示将原字符串重复n次。
let hello="Hello world!"
console.log( hello.repeat(12));
3. padStart(),padEnd()
padStart()用于头部补全
padEnd()用于尾部补全
//参数字符串,n表示补完一共多少个数 n=1开始算
let x= "xx";
console.log(x.padStart(4,"ab"))
console.log(x.padStart(3,"ab"))
console.log(x.padStart(7,"ab"))
console.log(x.padEnd(4,"ab"))
console.log(x.padEnd(9,"ab"))
4. trim(),trimStart(),trimEnd()
trim()消除字符串所有空格
trimStart()消除字符串头部的空格
trimEnd()消除尾部的空格
const s=" abc "
console.log(s)
console.log(s.trim())
console.log(s.trimStart())
console.log(s.trimEnd())