Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape R"()" in a raw string in C++

  string raw_str = R"(R"(foo)")";

If I have R"()" inside a raw string, and that causes the parser to confuse. (ie., it thought the left most )" was the end of the raw string.

How do I escape this?

like image 460
One Two Three Avatar asked Mar 21 '18 21:03

One Two Three


People also ask

What does \r do in string?

String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes).

What is raw string in C?

Raw string literal in C++ C++Server Side ProgrammingProgramming. In C++11 and above there is a concept called Raw string. In strings we use different characters like \n, \t etc. They have different meaning. The \n is used to return the cursor to the next line, the \t generates a tab etc.

What is a raw string?

A raw string in programming allows all characters in a string literal to remain the same in code and in the material, rather than performing their standard programming functions. Raw strings are denoted with the letter r, or capital R, and might look something like this: R “(hello)”

What does r in front of a string mean in C++?

A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \” ' of C++ are not processed. Hence, a raw string literal that starts with R”( and ends in )”.


2 Answers

The format for the raw-string literals[2] is: R"delimiter( raw_characters )delimiter"

so you can use a different delimiter that is not in the string like:

string raw_str = R"~(R"(foo)")~";
like image 84
Mihayl Avatar answered Sep 19 '22 16:09

Mihayl


The raw string will terminate after the first )" it sees. You can change the delimiter to *** for example:

string raw_str = R"***(R"(foo)")***";
like image 39
wally Avatar answered Sep 22 '22 16:09

wally