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
  • 301,070
  • 26
  • 186
  • 335
  • 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
  • 179,497
  • 17
  • 214
  • 278
3

You have several ways, including:

  • Escaping: "\""
  • Raw string: R"(")" (since C++11)
Jarod42
  • 203,559
  • 14
  • 181
  • 302
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
  • 301,070
  • 26
  • 186
  • 335