Reputation: 15849
Trying to override a tostring 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 thing is, the result isn't aligned when printed out:
name = test
ID = 42
sec nr = 11
acc nr = 55
Trying to add \n just prints it out without formating. Guessing it has something to do with @"" which I'm using for multi-lining.
Would like it to print out :
name = test
ID = 42
sec nr = 11
acc nr = 55
Upvotes: 9
Views: 29806
Reputation: 345
String.Join(Environment.NewLine, value.Select(i => i)
where values is collection of strings
Upvotes: 1
Reputation: 32094
A 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
*/
Upvotes: 4
Reputation: 51719
The @ before the string switches off standard C# string formatting, try
return string.Format(" name = {0}\n ID = {1} \n sec nr = {2} \n acc nr = {3}",
string, int, int ,int); // types
You can't use the @ 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);
Upvotes: 6
Reputation: 117330
If you add spaces in front, it will be printed that way.
I normally 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 prettier 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);
Upvotes: 15