0

I'm curious if I can implement a function like find in std :: string to search insensitive.

The function below find insensitive i want to call this function like that:

    int main()
    {
        std::string s1 = "do SoMething";
        std::string s2 = "something";
        auto found = s1.findCaseInsensitive(s2);
    //or 
        auto found  = std::findCaseInsensitive(s1,s2);
    }
    size_t findCaseInsensitive(std::string data, std::string toSearch)
    {
        std::transform(data.begin(), data.end(), data.begin(), ::tolower);
        std::transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);
        return data.find(toSearch);
    }



    namespace std{

    //...

    }
Ionut Alexandru
  • 680
  • 5
  • 17
  • No, you can't do that. Users are not allowed to add member functions to `std::string`. – cigien Apr 08 '20 at 18:46
  • Yes that was my question or if i can add a function like that under namespace `std` – Ionut Alexandru Apr 08 '20 at 18:49
  • I think [this](https://stackoverflow.com/questions/31850978/how-can-i-add-member-functions-in-built-in-classes-in-c) answers your question – cigien Apr 08 '20 at 18:55
  • i just edit a little bit my question, maybe is it possible to add this function under namespace std – Ionut Alexandru Apr 08 '20 at 18:59
  • 1
    Definitely not. There are very [few](https://en.cppreference.com/w/cpp/language/extending_std) things that you can add to std. Don't do it lightly. – cigien Apr 08 '20 at 19:03
  • There *are* better solutions than what you have. See [this](https://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find) for some ideas. – cigien Apr 08 '20 at 19:05

0 Answers0