除去模板字符串,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>