I want to check is some text is in a string for instance i have a string
str = "car, bycicle, bus"
and I have another string
str2 = "car"
I want to check if str2 is in str.
I am a newbie in javascript so please bear with me :)
Regards
The includes() method returns true if a string contains a specified string. Otherwise it returns false .
The simplest way to check if a string contains a substring in Python is to use the in operator. This will return True or False depending on whether the substring is found. For example: sentence = 'There are more trees on Earth than stars in the Milky Way galaxy' word = 'galaxy' if word in sentence: print('Word found.
Using in operator The Pythonic, fast way to check for the specific character in a string uses the in operator. It returns True if the character is found in the string and False otherwise. ch = '. '
Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.
if(str.indexOf(str2) >= 0) { ... }
Or if you want to go the regex route:
if(new RegExp(str2).test(str)) { ... }
However you may face issues with escaping (metacharacters) in the latter, so the first route is easier.
ES5
if(str.indexOf(str2) >= 0) { ... }
ES6
if (str.includes(str2)) { }
str.lastIndexOf(str2) >= 0;
this should work. untested though.
let str = "car, bycicle, bus";
let str2 = "car";
console.log(str.lastIndexOf(str2) >= 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With