How to convert std::string to lower case?

后端 未结 26 1734
旧时难觅i
旧时难觅i 2020-11-22 00:01

I want to convert a std::string to lowercase. I am aware of the function tolower(), however in the past I have had issues with this function and it

26条回答
  •  别那么骄傲
    2020-11-22 00:39

    My own template functions which performs upper / lower case.

    #include 
    #include 
    
    //
    //  Lowercases string
    //
    template 
    std::basic_string lowercase(const std::basic_string& s)
    {
        std::basic_string s2 = s;
        std::transform(s2.begin(), s2.end(), s2.begin(), tolower);
        return std::move(s2);
    }
    
    //
    // Uppercases string
    //
    template 
    std::basic_string uppercase(const std::basic_string& s)
    {
        std::basic_string s2 = s;
        std::transform(s2.begin(), s2.end(), s2.begin(), toupper);
        return std::move(s2);
    }
    

提交回复
热议问题