New line in multi-line

Trying to override tostring string in one of my classes.

return string.Format(@" name = {0} ID = {1} sec nr = {2} acc nr = {3}", string, int, int ,int); // types 

But the fact is that the result is not aligned when printed:

 name = test ID = 42 sec nr = 11 acc nr = 55 

Trying to add \ n just prints it without formatting. Guess this has to do with @ ", which I use for multi-line laying.

I would like to print:

 name = test ID = 42 sec nr = 11 acc nr = 55 
+7
string c # multiline
source share
5 answers

If you add spaces in front, they will be printed this way.

I usually do it like this.

  return string.Format( @" name = {0} ID = {1} sec nr = {2} acc nr = {3}", string, int, int ,int); // types 

Update: perhaps a more beautiful alternative:

 string[] lines = { " name = {0}", " ID = {1}", " sec nr = {2}", " acc nr = {3}" }; return string.Format( string.Join(Environment.Newline, lines), arg0, arg1, arg2, arg3); 
+14
source share

@ before the line disables the standard C # line formatting, try

  return string.Format(" name = {0}\n ID = {1} \n sec nr = {2} \n acc nr = {3}", string, int, int ,int); // types 

You cannot use @ and use \ n, \ t etc.

EDIT

This is IMHO - as good as it gets

  return string.Format("name = {0}\n" + "ID = {1}\n" + "sec nr = {2}\n" + "acc nr = {3}", string, int, int ,int); 
+5
source share

Solution from msdn:

 // Sample for the Environment.NewLine property using System; class Sample { public static void Main() { Console.WriteLine(); Console.WriteLine("NewLine: {0} first line{0} second line{0} third line", Environment.NewLine); } } /* This example produces the following results: NewLine: first line second line third line */ 
+4
source share
 String.Join(Environment.NewLine, value.Select(i => i) 

where the values ​​are a set of strings

+1
source share

No, all the places you have there.

0
source share

All Articles