JavaScript Check if String contains a substring

摘要: In JavaScript, we can use the classic indexOf() or the new ES6 includes to check if a String contains a substring.

In JavaScript, we can use the classic indexOf() or the new ES6 includes to check if a String contains a substring.

        // old school, classic way
        var str = "mkyong";
        if (str.indexOf('yo') > -1) { // returns `-1` if it is not present.
            console.log('match') // display this
        } else {
            console.log('not found')
        // ES6 only
        var str = "mkyong";
        console.log(str.includes('mk')); //true
        console.log(str.includes('mkg'));//false

上一篇: Spring Boot MySQL : Table DB_NAME.hibernate_sequence doesnt exist
下一篇: Spring Boot + Spring data JPA + MySQL
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章来源于互联网, 若有侵权, 联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布文章.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号