HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Case-insensitive string comparison in C++

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
stringcasecomparisoninsensitive

Problem

What is the best way of doing case-insensitive string comparison in C++ without transforming a string to all uppercase or all lowercase?

Please indicate whether the methods are Unicode-friendly and how portable they are.

Solution

Boost includes a handy algorithm for this:

#include 
// Or, for fewer header dependencies:
//#include 

std::string str1 = "hello, world!";
std::string str2 = "HELLO, WORLD!";

if (boost::iequals(str1, str2))
{
    // Strings are identical
}

Code Snippets

#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
}

Context

Stack Overflow Q#11635, score: 336

Revisions (0)

No revisions yet.