c++ - Case insensitive string::find -
is there exist case insensitive find() method std::string?
you upper-case both strings , use regular find. (note: approach may not correct if have unicode string.)
in boost there's ifind_first
case-insensitive search. (note returns range instead of size_t
).
#include <string> #include <boost/algorithm/string/find.hpp> #include <cstdio> #include <cctype> std::string uppercase(std::string input) { (std::string::iterator = input.begin(); != input.end(); ++ it) *it = toupper(*it); return input; } int main () { std::string foo = "1 foo 2 foo"; std::string target = "foo"; printf("string.find: %zu\n", foo.find(target)); printf("string.find w/ uppercase: %zu\n", uppercase(foo).find(uppercase(target))); printf("ifind_first: %zu\n", boost::algorithm::ifind_first(foo, target).begin() - foo.begin()); return 0; }
Comments
Post a Comment