How to check that an element is in a std::set?
The typical way to check for existence in many STL containers such as std::map, std::set, … is: const bool is_in = container.find(element) != container.end();
The typical way to check for existence in many STL containers such as std::map, std::set, … is: const bool is_in = container.find(element) != container.end();
Use: if my_item in some_list: … Also, inverse operation: if my_item not in some_list: … It works fine for lists, tuples, sets and dicts (check keys). Note that this is an O(n) operation in lists and tuples, but an O(1) operation in sets and dicts.
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)) The latter is a regular expression … Read more
jQuery has a utility function for this: $.inArray(value, array) Returns index of value in array. Returns -1 if array does not contain value. See also How do I check if an array includes an object in JavaScript?
Now with PHP 8 you can do this using str_contains: if (str_contains(‘How are you’, ‘are’)) { echo ‘true’; } RFC Before PHP 8 You can use the strpos() function which is used to find the occurrence of one string inside another one: $a=”How are you?”; if (strpos($a, ‘are’) !== false) { echo ‘true’; } Note … Read more
You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use: string title = “STRING”; bool contains = title.IndexOf(“string”, StringComparison.OrdinalIgnoreCase) >= 0; Even better is defining a new extension method for string: public static class StringExtensions { public static bool Contains(this string source, string toCheck, StringComparison comp) { return source?.IndexOf(toCheck, … Read more
Use the in operator: if “blah” not in somestring: continue