重学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则是查找以什么结尾的字符。
    console.log(str.startsWith('a',1)); //true
    console.log(str.endsWith('o',3)); // true,前三个字符‘mao’当中是以o结尾的。
    // 都可以传参数,但是不一样的是。startsWith是以索引0开头查找。而endsWith是以索引1开头查找。
</script>
</html>

 

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119862167

posted @ 2022-06-15 10:47  忘川信使  阅读(18)  评论(0编辑  收藏  举报