[英]What is exact analog of C#'s string.Compare ignoring case in C++?
Maybe somebody knows, what is exact C/C++ analog of C#'s string.Compare
ignoring case?也许有人知道,C# 的
string.Compare
的确切 C/C++ 模拟是什么。比较忽略大小写? Turned out, that _wcsicmp
differs, although both are supposed to use current locale or culture (which is en_US
).事实证明,
_wcsicmp
不同,尽管两者都应该使用当前的语言环境或文化(即en_US
)。
With string.Compare(..., true),
or string.Compare(..., StringComparison.CurrentCultureIgnoreCase),
or string.Compare(..., StringComparison.InvariantCultureIgnoreCase):
'~' before '+',
'=' before number,
letter before single quote
_wcsicmp
or wcsicmp_l
with explicit locale (LC_ALL, L"en_US")
puts them in opposite order. _wcsicmp
或wcsicmp_l
具有显式语言环境(LC_ALL, L"en_US")
将它们按相反的顺序排列。
I can reproduce it using character table, but maybe there is a better way.我可以使用字符表重现它,但也许有更好的方法。 Thanks!
谢谢!
In C there is a function for this in strings.h
called strcasecmp()
, but it is not portable.在 C 中有一个 function 在
strings.h
中称为strcasecmp()
,但它不是可移植的。 Although there is a Windows equivalent as noted in this answer: error C3861: 'strcasecmp': identifier not found in visual studio 2008?尽管有一个 Windows 等效项,如此答案中所述: 错误 C3861:'strcasecmp':在 Visual Studio 2008 中找不到标识符?
So you could write something like this.所以你可以写这样的东西。
#include <stdio.h>
#include <strings.h>
#ifdef _MSC_VER
//not #if defined(_WIN32) || defined(_WIN64) because we have strncasecmp in mingw
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
int main () {
char *str1 = "this is a string";
char *str2 = "THIS IS A STRING";
if (strcasecmp(str1, str2) == 0) printf("Strings match\n");
return 0;
}
If you really wanted, you could use this same approach with C++ strings by using their c_str()
function.如果你真的想要,你可以通过使用它们的
c_str()
function 对 C++ 字符串使用相同的方法。
strcasecmp(str1.c_str(), str2.c_str())
But really you'd be better off using boost::iequals(str1, str2)
但实际上你最好使用
boost::iequals(str1, str2)
#include <stdio.h>
#include <boost/algorithm/string.hpp>
int main () {
std::string str1 = "this is a string";
std::string str2 = "THIS IS A STRING";
if (boost::iequals(str1, str2)) printf("Strings match\n");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.