Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do multiline strings?

Tags:

c#

I am wondering how can you do multi line strings without the concat sign(+)

I tried this

 string a = String.Format(@"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
                                            {0}xxxxxxx", "GGG");

            string b = @"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
                                            xxxxxxx";


            string c = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
                                            + "xxxxxxx";
           Console.WriteLine(a);
           Console.WriteLine(b);
           Console.WriteLine(c);

This works but the first 2 render alot of whitespace. Is there away to write multi lines without the concat sign and ignore end whitespace without using something like replace after the string is rendered?

Edit

using System; using
System.Collections.Generic; using
System.Linq; using System.Text;

namespace ConsoleApplication2 {
    public class Test
    {

        public Test()
        {
        }

        public void MyMethod()
        {
            string someLongString = "On top of an eager overhead weds a
pressed vat.  How does a chance cage
the contract? The glance surprises the
radical wild.";
        }

} }

Look how it goes right though my formating of the braces and is sticking out(sorry kinda hard to show on stack but it is sticking way to far out).

Sad thing is notepad++ does a better job and when it wraps it goes right under the variable.

like image 342
chobo2 Avatar asked Dec 12 '22 12:12

chobo2


1 Answers

Just remove the white space. It may not be pretty, but it works:

string a = @"this is the first line
and this is the second";

Note that the second line needs to go all the way to the left margin:

multiline verbatim string

As an alternative, you can use the \n character, but then you can't use verbatim strings (the @-prefix):

string a = "first line\nsecond line";
like image 156
Fredrik Mörk Avatar answered Dec 29 '22 05:12

Fredrik Mörk