Case insensitive std::string.find()

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

    The new C++11 style:

    #include 
    #include 
    #include 
    
    /// Try to find in the Haystack the Needle - ignore case
    bool findStringIC(const std::string & strHaystack, const std::string & strNeedle)
    {
      auto it = std::search(
        strHaystack.begin(), strHaystack.end(),
        strNeedle.begin(),   strNeedle.end(),
        [](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
      );
      return (it != strHaystack.end() );
    }
    

    Explanation of the std::search can be found on cplusplus.com.

提交回复
热议问题