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 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()
.
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.
Related Topics
How to Get a Date Without Time in Java
How to Turn a String into a Inputstreamreader in Java
Com.Jcraft.Jsch.Jschexception: Unknownhostkey
Use Cases and Examples of Gof Decorator Pattern for Io
How to Set Specific Java Version to Maven
How to Detect the Presence of Url in a String
Killing Thread After Some Specified Time Limit in Java
How to Perform String Diffs in Java
Create Java Console Inside a Gui Panel
How to Convert Java String to Date Object
How to Check If Two Words Are Anagrams
Noclassdeffounderror on Maven Dependency
Java: Detect Duplicates in Arraylist
What Is the Use of Marker Interfaces in Java
Jaxb: How to Ignore Namespace During Unmarshalling Xml Document
How to Define Custom Exception Class in Java, the Easiest Way