Mario
Mario

Reputation: 95

Break code line with Console.WriteLine

I can't find how to split a Console.WriteLine text without creating a new line in the program. I mean, my code line is too long and it's uncomforable to scroll horizontally in order to check it

Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla")

the result is only a line of text in the console (Might be split because of its length, but keeps being the same line)

into the same, but in different lines in the code, giving the same result. I've tried just splitting them with a new code of line as if it was common code like this:

Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla
BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla
BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla
BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla")

and the result keeps being a line of text in the console

But looks like it's not the right way.

Sorry if it's stupid. Thanks

Upvotes: 2

Views: 6568

Answers (5)

If you only are going to write Bla then do

for(int i = 0; i < /*Amount of Bla's here*/; i++){
Console.Write("Bla");
}

Upvotes: -2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236288

The best you can do is split long string into substrings and concat them before output:

    Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"
 + "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"
 + "BlaBlaBlaBlaBlaBlaBlaBlaBla")

Upvotes: 1

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" +
                  "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" +
                  "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" +
                  "BlaBla");

If you have Resharper installed hitting enter inside of a string will automatically format the new line and add the + for you.

Upvotes: 1

Igor
Igor

Reputation: 62258

You can use Console.Write instead and call it multiple times. Call WriteLine once at the end either as a part of the all or with an empty string to ensure you start on a new line when you are done with that string.

Console.Write("BlaBlaBlaBlaBlaBlaBlaBla");
Console.Write("BlaBlaBlaBlaBlaBlaBlaBla");
Console.Write("BlaBlaBlaBlaBlaBlaBlaBla");
Console.Write("BlaBlaBlaBlaBlaBlaBlaBla");
Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBla");

Upvotes: 1

D Stanley
D Stanley

Reputation: 152626

There's not a way to split a string literal in C# without embedding the line breaks in the string. The typical way to split a line like that is:

Console.WriteLine("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" + 
                  "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" + 
                  "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla" + 
                  "BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla");

If your string has line breaks, then you can use the literal string identifier @:

Console.WriteLine(@"This is one line followed by a carriage return 
this is the second line of the string
and this is the third line");

Upvotes: 8

Related Questions