c++ multiline string

To create a multiline string in C++, you can use either a raw string literal or concatenate multiple string literals together. Here are the steps to create a multiline string in C++:

  1. Using a Raw String Literal: To create a multiline string using a raw string literal, you need to enclose the string content between a pair of double quotes (") and a pair of parentheses (( and )). The opening parenthesis is followed by the letter R and a pair of double quotes. Inside the double quotes, you can include any characters, including newlines, without escaping them.

cpp std::string multilineString = R"( This is a multiline string. )";

In the above code, the std::string variable multilineString is assigned a value using a raw string literal. The content of the string spans multiple lines and is enclosed between R"( and )". The multiline string includes three lines of text.

  1. Concatenating String Literals: Alternatively, you can create a multiline string by concatenating multiple string literals together using the concatenation operator +. Each string literal represents a line of text, and the concatenation operator combines them into a single multiline string.

cpp std::string multilineString = "This is a " "multiline " "string.";

In the above code, the std::string variable multilineString is assigned a value by concatenating multiple string literals using the + operator. Each string literal represents a line of text, and the resulting string is a multiline string.

Both approaches allow you to create a multiline string in C++. Choose the one that best suits your needs and coding style.