string search case insensitive

Started by
1 comment, last by Extrarius 15 years, 9 months ago
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.
<SkilletAudio> Your framerate proves your lack of manhood
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);
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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement