Alex Rivera | Logout

How to write a string literal which contains double quotes

Asked 2010-04-08T03:02:03.187
14

I need to do the following C# code in C++ (if possible). I have to initialize a long string with lots of quotes and other stuff in it.

// C# verbatim string
const String _literal = @"I can use "quotes" inside here";
Edit
Report

1 Answer

35

That is not available in C++03 (the current standard).

That is part of the C++0x draft standard but that's not readily available just yet.

For now, you just have to quote it explicitly:

const std::string _literal = "I have to escape my quotes in \"C++03\"";

Once C++0x becomes reality, you'll be able to write:

const std::string _literal = R"(but "C++0x" has raw string literals)";

and when you need )" in your literal:

const std::string _literal = R"DELIM(more "(raw string)" fun)DELIM";
answered 2010-04-08T03:04:40.913

Your Answer