除去模板字符串,es6字符串还有那些新的方法?
<script>
let string='apply';
console.log(string.indexOf('a'));//0
console.log(string.lastIndexOf('p'));//2
// es6
// includes() 返回布尔值,判断是否找到
console.log(string.includes('a'));//true
console.log(string.includes('a',1));//第二个参数可选,为搜索的起始位置索引
// startsWith() 返回布尔值,判断在原字符串头部是否找到
// endsWith() 返回布尔值,判断在原字符串尾部是否找到
console.log(string.startsWith('p'));//头部指第一个 false
console.log(string.startsWith('a'));//头部指第一个 true
console.log(string.endsWith('y'));//尾部指最后一个 true
//以上均可传第二个参数以改变搜索的起始位置 不可用正则表达式
// 字符串重复
// repeat()
console.log('ICY,'.repeat(3));//ICY,ICY,ICY, 输出3遍ICY
console.log('ICY'.repeat(2.9));//参数如果有小数,则向下取整
// 字符串补全
// padStart()
// padEnd()
console.log('ly'.padStart(3,'a'));//第一个参数 补齐后的总长度 从1开始 第二个参数 补齐的内容
// es6模板字符串
//“标签模板”的一个重要应用,就是过滤HTML字符串,防止用户输入恶意内容
// 基本用法
let Nickname=`zhangsan'\n'zhaosi`;
console.log(Nickname);//zhangsan'
// 'zhaosi
// 可以多行书写,并且会兼容空格
let something= `my name is zhangsan,
ahhh`;
console.log(something);//my name is zhangsan,
// ahhh
// ${} 括号种可以加入变量或者js表达式
let type='animal';
let color='red';
let strings=`color: ${color}
type: ${type}`;
console.log(strings);
//color: red
//type: animal
</script>