How can I create a string from a single character?

You can use any/all of the following to create a std::string from a single character:

  • std::string s(1, c);
    
    std::cout << s << std::endl;
    
  • std::string s{c};
    
    std::cout << s << std::endl;
    
  • std::string s;
    s.push_back(c);
    
    std::cout << s << std::endl;
    

Leave a Comment