[英]What is exact analog of C#'s string.Compare ignoring case in C++?
也許有人知道,C# 的string.Compare
的確切 C/C++ 模擬是什么。比較忽略大小寫? 事實證明, _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
或wcsicmp_l
具有顯式語言環境(LC_ALL, L"en_US")
將它們按相反的順序排列。
我可以使用字符表重現它,但也許有更好的方法。 謝謝!
在 C 中有一個 function 在strings.h
中稱為strcasecmp()
,但它不是可移植的。 盡管有一個 Windows 等效項,如此答案中所述: 錯誤 C3861:'strcasecmp':在 Visual Studio 2008 中找不到標識符?
所以你可以寫這樣的東西。
#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;
}
如果你真的想要,你可以通過使用它們的c_str()
function 對 C++ 字符串使用相同的方法。
strcasecmp(str1.c_str(), str2.c_str())
但實際上你最好使用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.