• Advertisement
Sign in to follow this  

string search case insensitive

This topic is 3519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement