case insensitive strings compare

Posted on 2003-03-02
Medium Priority
Last Modified: 2008-02-01
Hi, experts.
Recently, I've encountered a problem of comparing 2 strings containing German special symbols, like "oäüß"
and "ÖÄÜß". Inside the code i reffer to strings as usual ANSI. I used _stricmp - it doesn't work of course, then I tred to translate strings to Unicode and compare them:

 char* ch1 = "oäüß";
 char* ch2 = "ÖÄÜß";

 int len1 = strlen(ch1);
 int len2 = strlen(ch2);

 WCHAR mbt_out1[256];
 WCHAR mbt_out2[256];

 int nCodePage = 1252;//(I tried also CP_ACP = 0)
 MultiByteToWideChar(nCodePage, 0, ch1, len1, mbt_out1, len1);
 MultiByteToWideChar(nCodePage, 0, ch2, len2, mbt_out2, len2);

 int nCompRes = wcsicmp(mbt_out1, mbt_out2);

Nothing works, i.e. nCompRes != 0. My Windows is not German, but German language is installed.
Please, help who can. Thnx in advance.
Question by:alex_followap
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 86

Expert Comment

ID: 8052593
Try to set the CRT locale to German, e.g.

char* pszPrevLocale = setlocale( LC_ALL, "German" );

_stricmp ( string1, string2);

// restore locale settings
setlocale( LC_ALL, pszPrevLocale );


Expert Comment

ID: 8055703
for an excellent discussion of the topic see matt austern's "how to do case-insensitive string comparison" at,


this article, which was originally published in may 2000 issue of the c++ report, explains locales and facets and their use in c++ in a rather detailed manner and suggests two almost perfect comparators for general and performance use.  furthermore, it also explains exceptions such as german's B and SS conversion, etc.

Author Comment

ID: 8055971
Thank you, jkr. It works, but why after i translated strings into Unicode using MultiByteToWideChar it doesn't work? As far as i know, Unicode must be independent on locale.
LVL 86

Accepted Solution

jkr earned 1050 total points
ID: 8057506
>>but why after i translated strings into Unicode using MultiByteToWideChar it doesn't work?

I assume the codepage being the culprit. using CP_UTF8 *should* solve the problem. But, there is no need to use UNICODE at all.


Author Comment

ID: 8058898
Thank yu, it worked.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question