let str="lxy"; //字符串是否以某个字符开头 console.log(str.startsWith(‘l‘));//true console.log(str.startsWith(‘x‘));//false //字符串是否以某个字符结尾 console.log(str.endsWith(‘x‘));//false console.log(str.endsWith(‘y‘));//true //字符串是否包含某个字符 console.log(str.includes(‘x‘));//true console.log(str.includes(‘z‘));//false //repeat()重复某个字符串几次 console.log(str.repeat(3));//lxylxylxy console.log(str.repeat(5));//lxylxylxylxylxy
includes(),startsWith(),endsWith()都支持第2个参数。
使用第2个参数n时,endsWith的行为与其他两个方法有所不同。
var s="hello world!"; s.startsWith(‘world‘,6);//true s.endsWith(‘hello‘,5);//true s.includes(‘hello‘,6);//false
原文:https://www.cnblogs.com/chenhaiyun/p/14881378.html