Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a raw string?

Tags:

I came across this code snippet in C++17 draft n4713:

#define R "x" const char* s = R"y"; // ill-formed raw string, not "x" "y" 

What is a "raw string"? What does it do?

like image 295
S.S. Anne Avatar asked Jun 21 '19 20:06

S.S. Anne


People also ask

What does raw string mean in Python?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

How do you give a raw string?

In Python, strings prefixed with r or R , such as r'...' and r"..." , are called raw strings and treat backslashes \ as literal characters. Raw strings are useful when handling strings that use a lot of backslashes, such as Windows paths and regular expression patterns.

What is a raw string and an escape sequence?

Raw String Literal 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 )”.

What is the difference between normal string and raw string?

Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters. Raw strings are useful when you deal with strings that have many backslashes, for example, regular expressions or directory paths on Windows.


Video Answer


1 Answers

Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They’re useful for, say, encoding text like HTML. For example, contrast

"<a href=\"file\">C:\\Program Files\\</a>" 

which is a regular string literal, with

R"(<a href="file">C:\Program Files\</a>)" 

which is a raw string literal. Here, the use of parentheses in addition to quotes allows C++ to distinguish a nested quotation mark from the quotation marks delimiting the string itself.

like image 92
templatetypedef Avatar answered Oct 31 '22 02:10

templatetypedef