hi friends,
i would like to compare the strings irrespective of case type. so written the code like below.
var str1 =" HelloWorldlookatme";
var str2 = "world";
var compare1 = ((str1 !== undefined && str1 !== "") ? str1.toUpperCase() : "");
var compare2= ((str2 !== undefined && str2 !== "") ? str2.toUpperCase() : "");
var isContains = (compare1 .includes(compare2) && compare2.includes(compare1));
here if str1 is "HelloWorldlookatme" and str2 is "world" then its working but viceversa not working..
do we need to keep OR condtion instead of AND
if (isContains)
{alert("Exists");}
else
{ alert("NonExists");
}
followed above code.. still any other way to do..kindly let me know
thanks in advance
Tahir AnsariPosted Oct 16, 2023, 12:35 PM
you're using the
includesmethod with bothcompare1andcompare2, and you're using the logical AND (&&) to check if both strings include each other. This means both conditions need to be true for "Exists" to be alerted. If you want to check if either of the strings contains the other, you should use the logical OR (||) instead of AND.Murali PPosted Oct 16, 2023, 11:38 AM
hi friends,
i would like to compare the strings irrespective of case type. so written the code like below.
var str1 =" HelloWorldlookatme";
var str2 = "world";
var compare1 = ((str1 !== undefined && str1 !== "") ? str1.toUpperCase() : "");
var compare2= ((str2 !== undefined && str2 !== "") ? str2.toUpperCase() : "");
var isContains = (compare1 .includes(compare2) && compare2.includes(compare1));
here if str1 is "HelloWorldlookatme" and str2 is "world" then its working but viceversa not working..
do we need to keep OR condtion instead of AND
if (isContains)
{alert("Exists");}
else
{ alert("NonExists");
}
followed above code.. still any other way to do..kindly let me know
this code is not working if any of the string is empty.. kindly help me
thanks in advance
Tahir AnsariPosted Oct 16, 2023, 11:12 AM
you can also use regular expression