I am using std::string
's find()
method to test if a string is a substring of another. Now I need case insensitive version of the same thing. For string comparison I can always turn to stricmp()
but there doesn't seem to be a stristr()
.
I have found various answers and most suggest using Boost
which is not an option in my case. Additionally, I need to support std::wstring
/wchar_t
. Any ideas?
std::string provides a method std::string::find to search for the sub string inside a given string, but this function is case sensitive i.e.
To compare strings without case sensitivity, fold the values using tolower() or toupper() . Using tolower() or toupper() or the above in either order makes no difference when looking for equality, but can make an order difference.
Case-insensitive string comparison in using C++11 lambda function and equals() Logic is same as above, use std::equals() but instead of another global function use lambda function and make solution in single line i.e.
You could use std::search
with a custom predicate.
#include <locale> #include <iostream> #include <algorithm> using namespace std; // templated version of my_equal so it could work with both char and wchar_t template<typename charT> struct my_equal { my_equal( const std::locale& loc ) : loc_(loc) {} bool operator()(charT ch1, charT ch2) { return std::toupper(ch1, loc_) == std::toupper(ch2, loc_); } private: const std::locale& loc_; }; // find substring (case insensitive) template<typename T> int ci_find_substr( const T& str1, const T& str2, const std::locale& loc = std::locale() ) { typename T::const_iterator it = std::search( str1.begin(), str1.end(), str2.begin(), str2.end(), my_equal<typename T::value_type>(loc) ); if ( it != str1.end() ) return it - str1.begin(); else return -1; // not found } int main(int arc, char *argv[]) { // string test std::string str1 = "FIRST HELLO"; std::string str2 = "hello"; int f1 = ci_find_substr( str1, str2 ); // wstring test std::wstring wstr1 = L"ОПЯТЬ ПРИВЕТ"; std::wstring wstr2 = L"привет"; int f2 = ci_find_substr( wstr1, wstr2 ); return 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With