2009-07-01 138 views
16

我相信這會在之前被問過,但是找不到它。有沒有內置(即使用std :: wstring的方法或算法)的方式來區分大小寫比較兩個wstring對象?比較wstring和忽略案例

+6

注,即不區分大小寫的比較是依賴語言環境。 – avakar 2009-07-01 09:24:31

+1

看到http://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c,我會推薦升壓解決方案或提取c_str和使用wcscasecmp/_wcsicmp – Hasturkun 2009-07-01 09:35:53

+0

@Hasturkun:感謝您的鏈接。我隱約想起在SO上讀這個。 – Naveen 2009-07-01 10:04:59

回答

27

如果你不介意被捆綁到你可以使用<string.h>

int _wcsnicmp(
    const wchar_t *string1, 
    const wchar_t *string2, 
    size_t count 
); 

這個函數但是微軟實現,如果你想要最好的性能/兼容性/功能比你可能不得不看看boost庫(它的一部分是stl)。簡單的例子(從different answer採取不同的問題):

#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 
} 
2

您可以使用std :: tolower()將字符串轉換爲小寫或使用函數wcscasecmp對c_str進行不區分大小寫的比較。

這裏是一個比較仿函數就可以直接使用,以及:

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 
    } 
}; 
+1

我認爲要求提供標準庫解決方案。 – 2009-07-01 09:30:26

6

使用標準庫:

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 
} 
+11

if(stringA == stringB)強制我發表評論! :) 應該返回(stringA == stringB) – 2009-07-01 09:37:02

+4

此解決方案將不會在多個語言環境中工作,在某些語言中,當您轉換爲大寫字母,然後返回時會得到不同的字符串。 – Stan 2009-07-01 09:40:21

-1

可以使用不匹配()或lexicographical_compare()。這是由斯科特邁爾斯建議在Effecitve STL,商品35

2
#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; 
} 
2

談英語吧?!雖然我可以用我的可愛的升壓:)

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; 
} 
1

如果你需要(!使用運營商時==或=),那麼一個可能的解決方案是重新定義char_traits串總會讓不區分大小寫比較 - 去: :比較方法。

定義您自己的結構。例如

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... 
    } 
}; 

然後,定義自己的不區分大小寫字符串:

typedef std::basic_string< wchar_t, my_wchar_traits> my_wstring;