重學(xué)ES系列之字符串方面的處理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字符串方面的處理</title>
</head>
<body>
</body>
<script>
let str = 'maomin';
console.log(str.indexOf('x')); //-1
console.log(str.includes('x')); //false
// includes 是indexOf的升級版,如果不存在字符直接返回false。
console.log(str.startsWith('m')); //true
console.log(str.endsWith('n')); //true
// startsWith 是查找以什么開頭的字符。endsWith則是查找以什么結(jié)尾的字符。
console.log(str.startsWith('a',1)); //true
console.log(str.endsWith('o',3)); // true,前三個字符‘mao’當(dāng)中是以o結(jié)尾的。
// 都可以傳參數(shù),但是不一樣的是。startsWith是以索引0開頭查找。而endsWith是以索引1開頭查找。
</script>
</html>
作者:Vam的金豆之路
主要領(lǐng)域:前端開發(fā)
我的微信:maomin9761
微信公眾號:前端歷劫之路