enb081
enb081

Reputation: 4061

Multiline string with added text from variables

I am aware this will work:

string multiline_text = @"this is a multiline text
this is line 1
this is line 2
this is line 3";

How can I make the following work:

string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";

string multiline_text = @"this is a multiline text
this is " + a1 + " 
this is " + a2 + " 
this is " + a3 + ";

Is it possible without splitting the string into several substring, one for each line?

Upvotes: 52

Views: 49930

Answers (8)

Guru Stron
Guru Stron

Reputation: 143463

Since C# 11 and .NET 7 raw string literals are available which support interpolation:

string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";

string multiline_text = $"""
this is a multiline text
this is {a1} 
this is {a2}
this is {a3}
""";

Upvotes: 1

Ondrej Petrzilka
Ondrej Petrzilka

Reputation: 1579

With C# 6 (Visual Studio 2015), you can write:

string multiline_text = $@"this is a multiline text
this is {a1}
this is {a2}
this is {a3}";

String.Format will be used by compiler (like in Jons answer), but it's easier to read.

Upvotes: 5

Dennis Allen
Dennis Allen

Reputation: 412

For fun, you can also use an array join technique to get something readable and control indenting. Sometimes the multi-line template forcing you to fully left align is unsightly...

string a1 = "London", a2 = "France", a3 = "someone's underpants";

string result = string.Join(Environment.NewLine, new[] {
    $"this is {a1}", // interpolated strings look nice
    "this is " + a2, // you can concat if you can't interpolate
    $"this is {a3}"  // these in-line comments can be nice too
});

if you must format, wrap the join for a tidy result instead of individual lines.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503839

One option is to use string formatting instead. Before C# 6:

string pattern = @"this is a multiline text
this is {0}
this is {1}
this is {2}";

string result = string.Format(pattern, a1, a2, a3);

With C# 6, you can use an interpolated verbatim string literal:

string pattern = $@"this is a multiline text
this is {a1}
this is {a2}
this is {a3}";

Note that $@ has to be exactly that - if you try to use @$, it won't compile.

Upvotes: 125

Mike Perrenoud
Mike Perrenoud

Reputation: 67928

Another possible solution that goes along the lines of Jon Skeet's answer is this:

string result = string.Format(
    "this is a multiline text{0}this is {1}{0}this is {2}{0}this is {3}",
    Environment.NewLine, a1, a2, a3);

and so basically you're inserting a new line where you want one with {0}. It makes the string a little more abstract because of that, but the benefit of this solution is that it's fully encapsulated within the string.Format method. It's probably not all that relevant -but worth mentioning.

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56459

Although string.Format is better practice, to do what you're trying to achieve, just add the extra @s at the end of each line:

string multiline_text = @"this is a multiline text
this is " + a1 + @" 
this is " + a2 + @" 
this is " + a3 + @"";

You were also missing a last " before the ending semi colon.

Upvotes: 10

For some reason c# doesn't support multi-line text like that. the closest you'll get is:

string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";

string multiline_text = @"this is a multiline text" +
"this is " + a1 +
"this is " + a2 +
"this is " + a3;

Upvotes: 0

Chris Dixon
Chris Dixon

Reputation: 9167

You can gain readability like this from the StringBuilder class:

StringBuilder sb = new StringBuilder();
sb.AppendLine("this is a multiline");
sb.AppendLine("this is " + a1); // etc

var result = sb.ToString();

Upvotes: 5

Related Questions