Case insensitive std::string.find()

前端 未结 10 1981
挽巷
挽巷 2020-11-27 02:47

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 s

10条回答
  •  清酒与你
    2020-11-27 03:18

    You could use std::search with a custom predicate.

    #include 
    #include 
    #include 
    using namespace std;
    
    // templated version of my_equal so it could work with both char and wchar_t
    template
    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
    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(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;
    }
    

提交回复
热议问题