How to do case insensitive string comparison?
The simplest way to do it (if you're not worried about special Unicode characters) is to call toUpperCase
:
var areEqual = string1.toUpperCase() === string2.toUpperCase();
Case-insensitive string comparison in C++
Boost includes a handy algorithm for this:
#include <boost/algorithm/string.hpp>
// Or, for fewer header dependencies:
//#include <boost/algorithm/string/predicate.hpp>
std::string str1 = "hello, world!";
std::string str2 = "HELLO, WORLD!";
if (boost::iequals(str1, str2))
{
// Strings are identical
}
How do I do a case-insensitive string comparison?
Assuming ASCII strings:
string1 = 'Hello'
string2 = 'hello'
if string1.lower() == string2.lower():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")
As of Python 3.3, casefold() is a better alternative:
string1 = 'Hello'
string2 = 'hello'
if string1.casefold() == string2.casefold():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")
If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.
How to do a case-insensitive string comparison?
If you can afford deviating a little from strict C
standard, you can make use of strcasecmp()
.
It is a POSIX API.
Otherwise, you always have the option to convert the strings to a certain case (UPPER or lower) and then perform the normal comparison using strcmp()
.
Case insensitive string comparison
This is fairly simple; you just need to call strtolower()
on both variables.
If you need to deal with Unicode or international character sets, you can use mb_strtolower()
.
Please note that other answers suggest using strcasecmp()
—that function does not handle multibyte characters, so results for any UTF-8 string will be bogus.
Related Topics
How to Make Function Decorators and Chain Them Together
What Do Lambda Function Closures Capture
Store Large Data or a Service Connection Per Flask Session
Input() Error - Nameerror: Name '...' Is Not Defined
How to Generate All Permutations of a List
Get the Cartesian Product of a Series of Lists
Why Does My Recursive Function Return None
How to Serve Static Files in Flask
Of the Many Findelement(S)/By Functions in Selenium, When Would You Use One Over the Other
Why Does "A == X or Y or Z" Always Evaluate to True
How to Select Rows from a Dataframe Based on Column Values
How to Check If String Input Is a Number
Difference Between Shallow Copy, Deepcopy and Normal Assignment Operation