How to convert std::string to lower case?

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

    Adapted from Not So Frequently Asked Questions:

    #include <algorithm>
    #include <cctype>
    #include <string>
    
    std::string data = "Abc";
    std::transform(data.begin(), data.end(), data.begin(),
        [](unsigned char c){ return std::tolower(c); });
    

    You're really not going to get away without iterating through each character. There's no way to know whether the character is lowercase or uppercase otherwise.

    If you really hate tolower(), here's a specialized ASCII-only alternative that I don't recommend you use:

    char asciitolower(char in) {
        if (in <= 'Z' && in >= 'A')
            return in - ('Z' - 'z');
        return in;
    }
    
    std::transform(data.begin(), data.end(), data.begin(), asciitolower);
    

    Be aware that tolower() can only do a per-single-byte-character substitution, which is ill-fitting for many scripts, especially if using a multi-byte-encoding like UTF-8.

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

    Is there an alternative which works 100% of the time?

    No

    There are several questions you need to ask yourself before choosing a lowercasing method.

    1. How is the string encoded? plain ASCII? UTF-8? some form of extended ASCII legacy encoding?
    2. What do you mean by lower case anyway? Case mapping rules vary between languages! Do you want something that is localised to the users locale? do you want something that behaves consistently on all systems your software runs on? Do you just want to lowercase ASCII characters and pass through everything else?
    3. What libraries are available?

    Once you have answers to those questions you can start looking for a soloution that fits your needs. There is no one size fits all that works for everyone everywhere!

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

    Code Snippet

    #include<bits/stdc++.h>
    using namespace std;
    
    
    int main ()
    {
        ios::sync_with_stdio(false);
    
        string str="String Convert\n";
    
        for(int i=0; i<str.size(); i++)
        {
          str[i] = tolower(str[i]);
        }
        cout<<str<<endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 00:39

    My own template functions which performs upper / lower case.

    #include <string>
    #include <algorithm>
    
    //
    //  Lowercases string
    //
    template <typename T>
    std::basic_string<T> lowercase(const std::basic_string<T>& s)
    {
        std::basic_string<T> s2 = s;
        std::transform(s2.begin(), s2.end(), s2.begin(), tolower);
        return std::move(s2);
    }
    
    //
    // Uppercases string
    //
    template <typename T>
    std::basic_string<T> uppercase(const std::basic_string<T>& s)
    {
        std::basic_string<T> s2 = s;
        std::transform(s2.begin(), s2.end(), s2.begin(), toupper);
        return std::move(s2);
    }
    
    0 讨论(0)
  • 2020-11-22 00:40

    On microsoft platforms you can use the strlwr family of functions: http://msdn.microsoft.com/en-us/library/hkxwh33z.aspx

    // crt_strlwr.c
    // compile with: /W3
    // This program uses _strlwr and _strupr to create
    // uppercase and lowercase copies of a mixed-case string.
    #include <string.h>
    #include <stdio.h>
    
    int main( void )
    {
       char string[100] = "The String to End All Strings!";
       char * copy1 = _strdup( string ); // make two copies
       char * copy2 = _strdup( string );
    
       _strlwr( copy1 ); // C4996
       _strupr( copy2 ); // C4996
    
       printf( "Mixed: %s\n", string );
       printf( "Lower: %s\n", copy1 );
       printf( "Upper: %s\n", copy2 );
    
       free( copy1 );
       free( copy2 );
    }
    
    0 讨论(0)
  • 2020-11-22 00:44

    Another approach using range based for loop with reference variable

    string test = "Hello World";
    for(auto& c : test)
    {
       c = tolower(c);
    }
    
    cout<<test<<endl;
    
    0 讨论(0)
提交回复
热议问题