Case insensitive std::string.find()

前端 未结 10 1983
挽巷
挽巷 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:31

    #include 
    using namespace std;
    
    template 
    struct ichar {
        operator charT() const { return toupper(x); }
        charT x;
    };
    template 
    static basic_string > *istring(basic_string &s) { return (basic_string > *)&s; }
    template 
    static ichar *istring(const charT *s) { return (ichar *)s; }
    
    int main()
    {
        string s = "The STRING";
        wstring ws = L"The WSTRING";
        cout << istring(s)->find(istring("str")) << " " << istring(ws)->find(istring(L"wstr"))  << endl;
    }
    

    A little bit dirty, but short & fast.

提交回复
热议问题