开发者

JQuery string contains check [duplicate]

开发者 https://www.devze.com 2023-01-16 05:20 出处:网络
This question already has answers here: How to check whether a string contains a substring in JavaScript?
This question already has answers here: How to check whether a string contains a substring in JavaScript? (3 answers) Closed 开发者_如何学Python3 years ago.

I need to check whether a string contains another string or not?

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

Which function do I use to find out if str1 contains str2?


You can use javascript's indexOf function.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    console.log(str2 + " found");
}


var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

sttr1.search(str2);

it will return the position of the match, or -1 if it isn't found.


Please try:

str1.contains(str2)


I use,

var text = "some/String"; text.includes("/") <-- returns bool; true if "/" exists in string, false otherwise.


If you are worrying about Case sensitive change the case and compare the string.

 if (stringvalue.toLocaleLowerCase().indexOf("mytexttocompare")!=-1)
        {

            alert("found");
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消