Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Raw string literals

Reference for C++ raw string literals R"(...)": no escape sequences, custom delimiters, multi-line text, regex and JSON examples, and pitfalls.

A raw string literal is a string literal in which backslashes and quotes are taken literally. You write R"( to open it and )" to close it, and everything in between is content, including newlines. This removes the "escape everything twice" problem in regular expressions, Windows paths, JSON, SQL, HTML and shell snippets.

Raw strings exist since C++11 and combine with the encoding prefixes: u8R"(...)", uR"(...)", UR"(...)", LR"(...)". If the text itself contains the closing sequence )", add a custom delimiter of up to 16 characters between the quote and the parenthesis.

C++
const char* path = R"(C:\Users\Ada\notes.txt)";
const char* json = R"({"name": "Ada", "id": 7})";
const char* tricky = R"xyz(contains )" safely)xyz";

Try it

C++ Paths, regexes and multi-line text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Custom delimiters and embedded JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
R"delimiter( raw characters )delimiter"
u8R"(...)"   uR"(...)"   UR"(...)"   LR"(...)"
Part Description
R Marks the literal as raw. May follow an encoding prefix (u8, u, U, L).
delimiter Zero to 16 characters, chosen from the basic character set but not spaces, parentheses, backslash or control characters. Must be identical at both ends.
( ... ) The content. It ends at the first ) followed by the same delimiter and ".

Errors / undefined behavior: a literal that is not terminated, a delimiter longer than 16 characters, or a mismatched delimiter is a compile error. Like other string literals, a raw string literal has type const char[N] and must not be modified.

Usage notes

  • Escape sequences are not processed: R"(\n)" is two characters, a backslash and an n. The only text that has special meaning is the closing )delimiter".
  • Whitespace is preserved, including leading indentation of continuation lines and the newline right after R"(. Start the content on the same line as the opening if you do not want an initial newline.
  • Newline translation. The source file's newline is converted to \n by the compiler, even when the file uses CRLF, so the content is the same on every platform.
  • Delimiters are the way to embed )". Give them a meaningful name (json, sql, html) and your editor may highlight the content.
  • Preprocessing: raw strings are recognized before trigraphs, line splices (\ at end of line) and other translation phases are applied, so a backslash-newline in a raw string stays as is.
  • Not a std::string. The type is a character array; wrap it in std::string, use the s suffix (R"(text)"s), or std::string_view as shown.
  • Formatting with std::format works with raw literals too, but braces {} in the content are format placeholders: write {{ and }} for JSON braces.
  • Regular expressions: the raw literal removes only the C++ level of escaping; the regex engine still processes its own escapes.