Raw String Literals
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.”
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)