Charles Khunt
Charles Khunt

Reputation: 2505

Separating a large string

How do you say something like this?

static const string message = "This is a message.\n
                               It continues in the next line"

The problem is, the next line isn't being recognized as part of the string..

How to fix that? Or is the only solution to create an array of strings and then initialize the array to hold each line?

Upvotes: 4

Views: 279

Answers (3)

anon
anon

Reputation:

In C++ as in C, string litterals separated by whitespace are implicitly concatenated, so

"foo" "bar" 

is equivalent to:

"foobar"

So you want:

static const string message = "This is a message.\n"
                               "It continues in the next line";

Upvotes: 1

Alexander Torstling
Alexander Torstling

Reputation: 18898

You can use a trailing slash or quote each line, thus

"This is a message.\n \
 It continues in the next line"

or

"This is a message."
"It continues in the next line"

Upvotes: 9

RichieHindle
RichieHindle

Reputation: 281695

Enclose each line in its own set of quotes:

static const string message = "This is a message.\n"
                              "It continues in the next line";

The compiler will combine them into a single string.

Upvotes: 16

Related Questions