C++中不区分大小写的字符串比较
问题描述:
在 C++ 中进行不区分大小写的字符串比较而不将字符串转换为全部大写或全部小写的最佳方法是什么?
What is the best way of doing case-insensitive string comparison in C++ without transforming a string to all uppercase or all lowercase?
请说明这些方法是否对 Unicode 友好以及它们的可移植性如何.
Please indicate whether the methods are Unicode-friendly and how portable they are.
答
Boost 包含一个方便的算法:
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
}