判断字符串中是否含有某一个字符或字符串

      <script type="text/javascript">
            //判断字符串是否包含某个字符或字符串
            /*
             * js里面有indexof()方法
             * es6中新增了三种方法,includes()、startsWith()、endsWith()
             * 
             */

            var str1 = 'hello good byebye!';
            var str2 = "by";

            console.log(str1.includes(str2)); //true
            console.log(str1.startsWith(str2)); //false
            console.log(str1.endsWith(str2)); //false
            console.log(str1.startsWith("hello")); //true
            console.log(str1.endsWith("byebye!")); //true
        </script>

 

posted @ 2019-09-28 08:45  氧化成风  阅读(2169)  评论(0)    收藏  举报