Search code examples
c++stringcharinitializationstring-literals

Use the " character in a string


Hello I would like to use the character "in a string variable like this:

std::string hello = """;

Is it possible to do such a thing or am I talking nonsense? Thanks in advance!


Solution

  • You can either use the escaped character like

        std::string hello( "\"" );
    

    or

        std::string hello = "\"";
    

    or use a constructor that accepts a character literal like

    std::string hello( 1, '"' );
    

    Or you can use even a raw string literal like

    std::string hello( R"(")" );
    

    or

    std::string hello = R"(")";