判斷字符串中是否含有某一個字符或字符串


      <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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM