0

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!

Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
  • 2
    Reading your C++ book or [documentation](https://en.cppreference.com/w/cpp/language/string_literal) is recommended in such cases. – Marek R Jun 09 '21 at 10:11
  • duplicates: [Printing variable in quotation marks C++](https://stackoverflow.com/q/53110030/995714), [Include double-quote (") in C-string](https://stackoverflow.com/q/20458489/995714) – phuclv Jun 09 '21 at 10:18

3 Answers3

3

Just escape it:

std::string hello = "\"";
David Schwartz
  • 173,634
  • 17
  • 200
  • 267
3

You have several ways, including:

  • Escaping: "\""
  • Raw string: R"(")" (since C++11)
Jarod42
  • 190,553
  • 13
  • 166
  • 271
2

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"(")";
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303