Sign in to follow this  
AcidZombie24

string search case insensitive

Recommended Posts

Quote:
Original post by AcidZombie24
i like std::string but i want a way to search w/o case sensitivity. What is a good way to do this? note, both string and substr can be potentially come from external files.


The typical way to do this (in any language) is to simply convert all strings to upper case or convert all strings to lower case. This effectively makes comparisons case insensitive.

To convert a string to uppercase for example, you can do the following:


#include <cctype>
#include <algorithm>
#include <string>
...
std::transform(myString.begin(), myString.end(), myString.begin(), toupper);

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
[...]To convert a string to uppercase for example, you can do the following:
[...]
You don't want to use the old C "toupper". The closest thing you might want is std::toupper in the locale header, but realize that the very idea of 'case' is actually a very complicated issue and is not handled well in the C++ standard library. The transformation depends on the locale {which the C++ library gets right}, and there is not always a 1-to-1 relationship between an uppercase character and the lowercase representation {which the C++ library gets wrong}. Some locales contain characters that should be joined or split depending on numerous rules when changing case, but the C++ locale implementation only allows a 1-to-1 mapping between cases.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this