如何检查字符串是否包含子字符串? [重复]

本文翻译自:How do I check if string contains substring? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I have a shopping cart that displays product options in a dropdown menu and if they select "yes", I want to make some other fields on the page visible. 我有一个购物车,在下拉菜单中显示产品选项,如果他们选择“是”,我想让页面上的其他字段可见。

The problem is that the shopping cart also includes the price modifier in the text, which can be different for each product. 问题是购物车还包括文本中的价格修饰符,每个产品可能不同。 The following code works: 以下代码有效:

$(document).ready(function() {
    $('select[id="Engraving"]').change(function() {
        var str = $('select[id="Engraving"] option:selected').text();
        if (str == "Yes (+ $6.95)") {
            $('.engraving').show();
        } else {
            $('.engraving').hide();
        }
    });
});

However I would rather use something like this, which doesn't work: 但是,我宁愿使用这样的东西,这是行不通的:

$(document).ready(function() {
    $('select[id="Engraving"]').change(function() {
        var str = $('select[id="Engraving"] option:selected').text();
        if (str *= "Yes") {
            $('.engraving').show();
        } else {
            $('.engraving').hide();
        }
    });
});

I only want to perform the action if the selected option contains the word "Yes", and would ignore the price modifier. 如果所选选项包含单词“是”,我只想执行操作,并忽略价格修饰符。


#1楼

参考:https://stackoom.com/question/EbVT/如何检查字符串是否包含子字符串-重复


#2楼

Another way: 其他方式:

var testStr = "This is a test";

if(testStr.contains("test")){
    alert("String Found");
}

** Tested on Firefox, Safari 6 and Chrome 36 ** **在Firefox,Safari 6和Chrome 36上测试**


#3楼

You can use this Polyfill in ie and chrome 您可以在ie和chrome中使用此Polyfill

if (!('contains' in String.prototype)) {
    String.prototype.contains = function (str, startIndex) {
        "use strict";
        return -1 !== String.prototype.indexOf.call(this, str, startIndex);
    };
}

#4楼

Returns number of times the keyword is included in the string. 返回关键字包含在字符串中的次数。

var count = "I have one keyword".match(/keyword/g);
var clean_count = !count ? false : count.length;

#5楼

you can define an extension method and use it later. 您可以定义扩展方法并在以后使用它。

String.prototype.contains = function(it) 
{ 
   return this.indexOf(it) != -1; 
};

so that you can use in your page anywhere like: 这样你就可以在任何地方使用你的页面:

var str="hello how are you";
str.contains("are");

which returns true . 返回true

Refer below post for more extension helper methods. 有关更多扩展帮助方法,请参阅以下帖子。 Javascript helper methods Javascript帮助方法


#6楼

You can also check if the exact word is contained in a string. 您还可以检查字符串中是否包含确切的单词。 Eg: 例如:

function containsWord(haystack, needle) {
    return (" " + haystack + " ").indexOf(" " + needle + " ") !== -1;
}

Usage: 用法:

containsWord("red green blue", "red"); // true
containsWord("red green blue", "green"); // true
containsWord("red green blue", "blue"); // true
containsWord("red green blue", "yellow"); // false

This is how jQuery does its hasClass method. 这就是jQuery如何使用hasClass方法。