Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:58:49+00:00 2026-06-18T02:58:49+00:00

is it possible to make std::string always hold a lower-case string? here’s how I

  • 0

is it possible to make std::string always hold a lower-case string?
here’s how I would use it:

typedef std::basic_string<...> lowercase_string;

void myfunc()
{
  lowercase_string s = "Hello World"; // notice mixed case
  printf(s.c_str());                  // prints "hello world" in lowercase
  std::string s2 = s;
  printf(s2.c_str());                 // prints "hello world" in lowercase
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:58:50+00:00Added an answer on June 18, 2026 at 2:58 am

    You can write your own char traits and pass it to std::basic_string as second template argument.

    Here is a minimal example:

    template<typename T>
    struct lowercase_char_traits : std::char_traits<T>
    {
        static T* copy(T* dest, const T* src, std::size_t count )
        {
             for(size_t i = 0 ; i < count ; ++i)
                  dest[i] = std::tolower(src[i]);
             return dest;
        }
        static void assign(T & out, T in)
        {
           out = std::tolower(in);
        }
        //implement other overload of assign yourself
    
        //note that you may have to implement other functionality 
        //depending on your requirement
    };
    

    And then define a typedef as:

    typedef std::basic_string<char, lowercase_char_traits<char>> lowercase;
    

    And here is a test program:

    int main() 
    {
        lowercase s1 = "Hello World";
        std::cout << s1.c_str() << std::endl;
    
        lowercase s2 = "HELLO WORLD";
        std::cout << std::boolalpha << (s1 == s2) << std::endl;
    
        lowercase s3 = "HELLO";
        s3 += " WorL";
        s3.append("D");
        std::cout << std::boolalpha << (s1 == s3) << std::endl;
    
        std::cout << s2.c_str() << std::endl;
        std::cout << s3.c_str() << std::endl;
    }
    

    Output:

    hello world
    true
    true
    hello world
    hello world
    

    Cool, isn’t it?

    • Online demo

    Note that to have a fully-working lowercase string class, you may need to define other functionality of lowercase_char_traits also, depending on what behavior you want out of such class.

    Have a look at the Herb Sutter brilliant article for details and explanation:

    • So you want a case-insensitive string class? Your mission, should you choose to accept it, is to write one.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Case insensitive std::string.find() I want to make use of the std::string::find() method
Possible Duplicate: string c_str() vs. data() I use strncpy(dest, src_string, 32) to convert std::string
std::find_if takes a predicate in one of it's overloaded function. Binders make it possible
Possible Duplicate: std::string x(x); class A {}; int main() { A a(a); } This
Possible Duplicate: std::string to float or double I am writing a calculator (learning C++),
for example if I have std::string test; I want to be make test constant
Referring to this web site http://www.cplusplus.com/reference/std/utility/make_pair/ The std::make_pair has this signature (and possible implementation):
Is it possible make some handler that will do something when user shutdown computer
Is it possible make an activity, that will lock the android device? That device
Possible Duplicate: Make error: missing separator I am so stressed out by this silly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.