Case insensitive standard string comparison in C++ -
this question has answer here:
void main() { std::string str1 = "abracadabra"; std::string str2 = "abracadabra"; if (!str1.compare(str2)) { cout << "compares" } }
how can make work? bascially make above case insensitive. related question googled , here
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
there case insensitive method string::compare(str1, str2, bool). question how related way doing.
you can create predicate function , use in std::equals
perform comparison:
bool icompare_pred(unsigned char a, unsigned char b) { return std::tolower(a) == std::tolower(b); } bool icompare(std::string const& a, std::string const& b) { if (a.length()==b.length()) { return std::equal(b.begin(), b.end(), a.begin(), icompare_pred); } else { return false; } }
now can do:
if (icompare(str1, str)) { std::cout << "compares" << std::endl; }
Comments
Post a Comment