Raw String Literals

发布时间 2023-07-20 15:53:04作者: 冰山奇迹

Raw string literals are string literals that can span multiple lines of code, they don’t require escaping
of embedded double quotes, and escape sequences like \t and \n are processed as normal text and
not as escape sequences. Escape sequences are discussed in Chapter 1, “A Crash Course in C++ and
the Standard Library.” For example, if you write the following with a normal string literal, you will
get a compilation error because the string contains non-escaped double quotes:
  const char* str { "Hello "World"!" }; // Error!

Normally you have to escape the double quotes as follows:
  const char* str { "Hello \"World\"!" };
With a raw string literal, you can avoid the need to escape the quotes. A raw string literal starts with
R"( and ends with )":
  const char* str { R"(Hello "World"!)" };
If you need a string consisting of multiple lines, without raw string literals, you need to embed \n
escape sequences in your string where you want to start a new line. Here’s an example:
  const char* str { "Line 1\nLine 2" };
If you output this string to the console, you get the following:
  Line 1
  Line 2
With a raw string literal, instead of using \n escape sequences to start new lines, you can simply press
Enter to start real physical new lines in your source code as follows. The output is the same as the
previous code snippet using the embedded \n.
  const char* str { R"(Line 1
  Line 2)" };
Escape sequences are ignored in raw string literals. For example, in the following raw string literal,
the \t escape sequence is not replaced with a tab character but is kept as the sequence of a backslash
followed by the letter t:
  const char* str { R"(Is the following a tab character? \t)" };
So, if you output this string to the console, you get this:
  Is the following a tab character? \t
Because a raw string literal ends with )", you cannot embed a )" in your string using this syntax.
For example, the following string is not valid because it contains the )" sequence in the middle of
the string:
  const char* str { R"(Embedded )" characters)" }; // Error!
If you need embedded )" characters, you need to use the extended raw string literal syntax, which is
as follows:
  R"d-char-sequence(r-char-sequence)d-char-sequence"
The r-char-sequence is the actual raw string. The d-char-sequence is an optional delimiter
sequence, which should be the same at the beginning and at the end of the raw string literal. This
delimiter sequence can have at most 16 characters. You should choose this delimiter sequence as a
sequence that will not appear in the middle of your raw string literal.
The previous example can be rewritten using a unique delimiter sequence as follows:
  const char* str { R"-(Embedded )" characters)-" };
Raw string literals make it easier to work with database querying strings, regular expressions, file
paths, and so on. Regular expressions are discussed in Chapter 21, “String Localization and Regular
Expressions.”