How to convert std::string to lower case?

后端 未结 26 1668
旧时难觅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:23

    std::ctype::tolower() from the standard C++ Localization library will correctly do this for you. Here is an example extracted from the tolower reference page

    #include 
    #include 
    
    int main () {
      std::locale::global(std::locale("en_US.utf8"));
      std::wcout.imbue(std::locale());
      std::wcout << "In US English UTF-8 locale:\n";
      auto& f = std::use_facet>(std::locale());
      std::wstring str = L"HELLo, wORLD!";
      std::wcout << "Lowercase form of the string '" << str << "' is ";
      f.tolower(&str[0], &str[0] + str.size());
      std::wcout << "'" << str << "'\n";
    }
    

提交回复
热议问题