C++ 将 wstring 与忽略大小写进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1068134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing wstring with ignoring the case
提问by Naveen
I am sure this would have been asked before but couldn't find it. Is there any built in (i.e. either using std::wstring's methods or the algorithms) way to case insensitive comparison the two wstring objects?
我相信这之前会被问到,但找不到。是否有任何内置(即使用 std::wstring 的方法或算法)的方式来对两个 wstring 对象进行不区分大小写的比较?
回答by Stan
If you don't mind being tied to Microsoft implementation you can use this function defined in <string.h>
如果您不介意绑定到 Microsoft 实现,您可以使用定义在 <string.h>
int _wcsnicmp(
const wchar_t *string1,
const wchar_t *string2,
size_t count
);
But if you want best performance/compatibility/functionality ratio you will probably have to look at boost library (part of it is stl anyway). Simple example (taken from different answerto different question):
但是,如果您想要最佳的性能/兼容性/功能比,您可能必须查看 boost 库(无论如何它的一部分是 stl)。简单示例(取自不同问题的不同答案):
#include <boost/algorithm/string.hpp>
std::wstring wstr1 = L"hello, world!";
std::wstring wstr2 = L"HELLO, WORLD!";
if (boost::iequals(wstr1, wstr2))
{
// Strings are identical
}
回答by Callum
Using the standard library:
使用标准库:
bool comparei(wstring stringA , wstring stringB)
{
transform(stringA.begin(), stringA.end(), stringA.begin(), toupper);
transform(stringB.begin(), stringB.end(), stringB.begin(), toupper);
return (stringA == stringB);
}
wstring stringA = "foo";
wstring stringB = "FOO";
if(comparei(stringA , stringB))
{
// strings match
}
回答by Chris Harris
You can use std::tolower()
to convert the strings to lowercase or use the function wcscasecmp
to do a case insensitive compare on the c_str()
's.
您可以使用std::tolower()
将字符串转换为小写或使用该函数wcscasecmp
对c_str()
's进行不区分大小写的比较。
Here is a comparison functor you can use directly as well:
这是一个您也可以直接使用的比较函子:
struct ci_less_w
{
bool operator() (const std::wstring & s1, const std::wstring & s2) const
{
#ifndef _WIN32
return wcscasecmp(s1.c_str(), s2.c_str()) < 0;
#else
return _wcsicmp(s1.c_str(), s2.c_str()) < 0;
#endif
}
};
回答by AraK
Talking about English right ?! though I would go with my lovely Boost :)
说英语吧?!虽然我会和我可爱的 Boost 一起去:)
bool isequal(const std::wstring& first, const std::wstring& second)
{
if(first.size() != second.size())
return false;
for(std::wstring::size_type i = 0; i < first.size(); i++)
{
if(first[i] != second[i] && first[i] != (second[i] ^ 32))
return false;
}
return true;
}
回答by Maik Beckmann
#include <algorithm>
#include <string>
#include <cstdio>
bool icase_wchar_cmp(wchar_t a, wchar_t b)
{
return std::toupper(a) == std::toupper(b);
}
bool icase_cmp(std::wstring const& s1, std::wstring const& s2)
{
return (s1.size() == s2.size()) &&
std::equal(s1.begin(), s1.end(), s2.begin(),
icase_wchar_cmp);
}
int main(int argc, char** argv)
{
using namespace std;
wstring str1(L"Hello"), str2(L"hello");
wprintf(L"%S and %S are %S\n", str1.c_str(), str2.c_str(),
icase_cmp(str1,str2) ? L"equal" : L"not equal");
return 0;
}
回答by Sahas
You could use the boost string algorithms library. Its a header only library as long as you're not going to do regex. So you can do that very easily.
您可以使用 boost 字符串算法库。只要你不打算做正则表达式,它就是一个只有头文件的库。所以你可以很容易地做到这一点。
http://www.boost.org/doc/libs/1_39_0/doc/html/string_algo.html
http://www.boost.org/doc/libs/1_39_0/doc/html/string_algo.html
回答by C?t?lin Piti?
If you need that the string will always make case insensitive comparation (when using operators == or !=), then a possible elegant solution is to redefine char_traits::compare method.
如果您需要字符串始终进行不区分大小写的比较(使用运算符 == 或 != 时),那么一个可能的优雅解决方案是重新定义 char_traits::compare 方法。
Define your own structure. Example
定义自己的结构。例子
struct my_wchar_traits: public std::char_traits< wchar_t>
{
static int compare( const char_type* op1, const char_type* op2, std::size_t num)
{
// Implementation here... any of the previous responses might help...
}
};
Then, define your own case insensitive string:
然后,定义您自己的不区分大小写的字符串:
typedef std::basic_string< wchar_t, my_wchar_traits> my_wstring;
回答by ralphtheninja
You can use mismatch() or lexicographical_compare(). This is suggested by Scott Meyers in Effecitve STL, item 35.
您可以使用 mismatch() 或 lexicographical_compare()。这是 Scott Meyers 在 Effecitve STL 第 35 项中提出的建议。