C++ - Strings: Special Characters

Overview

Estimated time: 10–15 minutes

Represent newlines, tabs, quotes, backslashes, and multi-line text with escape sequences and raw string literals.

Learning Objectives

  • Use common escapes: \n, \t, \", \' and \\.
  • Write raw string literals for regexes and JSON snippets.

Prerequisites

Examples

#include <string>
#include <iostream>
int main(){
  std::string path = "C:\\temp\\file.txt";
  std::string json = R"({"name":"Ada","skills":["C++","Math"]})";
  std::cout << path << "\n" << json;
}

Common Pitfalls

  • Remember to double backslashes in normal string literals on Windows paths.