xxxxxxxxxx
single = "a"
many = "aaaaaaaa"
print(single in many)
#True since a is in aaaaaaaa
xxxxxxxxxx
var str = "We got a poop cleanup on isle 4.";
if(str.indexOf("poop") !== -1){
alert("Not again");
}
//use indexOf (it returns position of substring or -1 if not found)
xxxxxxxxxx
Like this:
if (str.indexOf("Yes") >= 0)
or you can use the tilde operator:
if (~str.indexOf("Yes"))
This works because indexOf() returns -1 if the string wasn't found at all.
Note that this is case-sensitive.
If you want a case-insensitive search, you can write
if (str.toLowerCase().indexOf("yes") >= 0)
Or:
if (/yes/i.test(str))
xxxxxxxxxx
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
xxxxxxxxxx
# User input
main_string = input("Enter the main string: ")
substring = input("Enter the substring to search for: ")
# Check if substring is present in the main string
if substring in main_string:
print("Substring found in the main string!")
else:
print("Substring not found.")