'in' operator in JavaScript. String comparison

This question already has an answer here:

  • How to check whether a string contains a substring in JavaScript? 49 answers

  • I think one way is to use String.indexOf()

    'aaa' .indexOf('a') > -1
    

    In javascript the in operator is used to check whether an object has a property


    你正在寻找indexOf

    'aaa'.indexOf('a') == 0 //if a char exists in the string, indexOf will return
                            // the index of the first instance of the char
    'aaa'.indexOf('b') == -1 //if a char doesn't exist in the string, indexOf will return -1
    

    Duplicate (How to check whether a string contains a substring in JavaScript?)

    Try this:

    var s = "aaaabbbaaa";
    var result = s.indexOf("a") > -1;
    
    链接地址: http://www.djcxy.com/p/2052.html

    上一篇: 检查一个必须包含另一个字符串的字符串

    下一篇: 'in'运算符在JavaScript中。 字符串比较