How to convert std::string to lower case?

后端 未结 26 1677
旧时难觅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:20
    // tolower example (C++)
    #include <iostream>       // std::cout
    #include <string>         // std::string
    #include <locale>         // std::locale, std::tolower
    
    int main ()
    {
      std::locale loc;
      std::string str="Test String.\n";
      for (std::string::size_type i=0; i<str.length(); ++i)
        std::cout << std::tolower(str[i],loc);
      return 0;
    }
    

    For more information: http://www.cplusplus.com/reference/locale/tolower/

    0 讨论(0)
  • 2020-11-22 00:20

    C++ doesn't have tolower or toupper methods implemented for std::string, but it is available for char. One can easily read each char of string, convert it into required case and put it back into string. A sample code without using any third party library:

    #include<iostream>
    
    int main(){
        std::string str = std::string("How IS The Josh");
        for(char &ch : str){
            ch = std::tolower(ch);
        }
        std::cout<<str<<std::endl;
        return 0;
    }
    

    For character based operation on string : For every character in string

    0 讨论(0)
  • 2020-11-22 00:21

    Using range-based for loop of C++11 a simpler code would be :

    #include <iostream>       // std::cout
    #include <string>         // std::string
    #include <locale>         // std::locale, std::tolower
    
    int main ()
    {
      std::locale loc;
      std::string str="Test String.\n";
    
     for(auto elem : str)
        std::cout << std::tolower(elem,loc);
    }
    
    0 讨论(0)
  • 2020-11-22 00:21

    If the string contains UTF-8 characters outside of the ASCII range, then boost::algorithm::to_lower will not convert those. Better use boost::locale::to_lower when UTF-8 is involved. See http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/conversions.html

    0 讨论(0)
  • 2020-11-22 00:21

    I tried std::transform, all i get is abominable stl criptic compilation error that only druids from 200 years ago can understand (cannot convert from to flibidi flabidi flu)

    this works fine and can be easily tweaked

    string LowerCase(string s)
    {
        int dif='a'-'A';
        for(int i=0;i<s.length();i++)
        {
            if((s[i]>='A')&&(s[i]<='Z'))
                s[i]+=dif;
        }
       return s;
    }
    
    string UpperCase(string s)
    {
       int dif='a'-'A';
        for(int i=0;i<s.length();i++)
        {
            if((s[i]>='a')&&(s[i]<='z'))
                s[i]-=dif;
        }
       return s;
    }
    
    0 讨论(0)
  • 2020-11-22 00:22

    Boost provides a string algorithm for this:

    #include <boost/algorithm/string.hpp>
    
    std::string str = "HELLO, WORLD!";
    boost::algorithm::to_lower(str); // modifies str
    

    Or, for non-in-place:

    #include <boost/algorithm/string.hpp>
    
    const std::string str = "HELLO, WORLD!";
    const std::string lower_str = boost::algorithm::to_lower_copy(str);
    
    0 讨论(0)
提交回复
热议问题