Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ multiline string literal

Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.

like image 951
rlbond Avatar asked Jul 16 '09 06:07

rlbond


People also ask

How do you define multi line string literals?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

How do you make a multiline string in C++?

The string str can be written in multiple lines by using two double quotes ( “ “ ) to break the string at any point in the middle. Then the string is displayed using puts. The code snippet that shows this is as follows.

How do you write a long string in C++?

In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. Below are few examples with long long strings broken using two double quotes for better readability. "letters and digits, but must start with a letter. "


2 Answers

Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:

const char *text =   "This text is pretty long, but will be "   "concatenated into just a single string. "   "The disadvantage is that you have to quote "   "each part, and newlines must be literal as "   "usual."; 

The indentation doesn't matter, since it's not inside the quotes.

You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:

 const char *text2 =   "Here, on the other hand, I've gone crazy \ and really let the literal span several lines, \ without bothering with quoting each line's \ content. This works, but you can't indent."; 

Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.

like image 123
unwind Avatar answered Sep 21 '22 16:09

unwind


In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.

const char * vogon_poem = R"V0G0N(              O freddled gruntbuggly thy micturations are to me                  As plured gabbleblochits on a lurgid bee.               Groop, I implore thee my foonting turlingdromes.               And hooptiously drangle me with crinkly bindlewurdles, Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.                  (by Prostetnic Vogon Jeltz; see p. 56/57) )V0G0N"; 

All the spaces and indentation and the newlines in the string are preserved.

These can also be utf-8|16|32 or wchar_t (with the usual prefixes).

I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put

                "(by Prostetnic Vogon Jeltz; see p. 56/57)" 

(note extra quotes) and the string above would still be correct. Otherwise I could just as well have used

const char * vogon_poem = R"( ... )"; 

The parens just inside the quotes are still needed.

like image 41
emsr Avatar answered Sep 20 '22 16:09

emsr