Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing wstring with ignoring the case

Tags:

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?

like image 341
Naveen Avatar asked Jul 01 '09 09:07

Naveen


2 Answers

If you don't mind being tied to Microsoft implementation you can use this function defined in <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 answer to different question):

#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 } 
like image 129
Stan Avatar answered Sep 28 '22 06:09

Stan


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 } 
like image 37
Callum Avatar answered Sep 28 '22 07:09

Callum