0

I'm iterating through a list of objects of type Persons, and displaying the name and surname of each persons.

for (int i = 0; i < myList.Count; i++)
{
   Console.WriteLine("-|- Name: " + myList[i].GetName() +
                      "| Surname: " + myList[i].GetName() + "|");

}

The output in the console is as follows!

-|- Name: Abcdef | Surname: Asqw |
-|- Name: Aswer | Surname: Asdgfsdf |
-|- Name: Adxz | Surname: Asdsada |

I would like the output to be shown in the form of a table as below.

-|- Name: Abcdef | Surname: Asqw     |
-|- Name: Aswer  | Surname: Asdgfsdf |
-|- Name: Adxz   | Surname: Asdsada  |

Thanks

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
user2307236
  • 665
  • 4
  • 19
  • 44

7 Answers7

3
var name = myList[i].GetName();
var surname = myList[i].GetName();
Console.WriteLine("-|- Name: {0,-10} | Surname: {1:-10} |", name, surname);

You can supply the padding parameter to the formatter. In this example, every column takes up at least 10 characters.

{0:-10}
  0   argument reference
  :   parameter separator
  -10 left-justified padding (exempting the `-` means it will pad to the right)

See the String.Format docs for more information (since Console.WriteLine(format, params args) inherits this composite formatting)

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

Use padRight to fill with spaces all the rows with less characters so you can align all of them at same location:

Console.WriteLine( ("-|- Name: " + myList[i].GetName()).PadRight(20,' ') + 
                  ("| Surname: " + myList[i].GetName() ) + "|".PadRight(20,' ');
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
1

C#'s string has a PadRight method (public string PadRight ( int totalWidth, char paddingChar );). You can use this method to fill a string with a char as long as required to have it be it certain length. Drawback: you need to know what the longest text will by, so you night need to loop twice over the data.

Sascha
  • 10,231
  • 4
  • 41
  • 65
1

You should use composite formatting as follows:

Console.WriteLine(
  "-|- Name: {0,-10} | Surname: {1,-10} |", 
  myList[i].GetName(),
  myList[i].GetName());

For more information, see http://msdn.microsoft.com/en-us/library/txafckwd.aspx

Kris Vandermotten
  • 10,111
  • 38
  • 49
0

You can add \t in your strings everytime you would like to add a tabulation.

Pacane
  • 20,273
  • 18
  • 60
  • 97
0
Console.WriteLine("-|- Name: {0,-10}| Surname: {1,-10}|", 
    myList[i].GetName(), 
    myList[i].GetSurname());
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
0

You can use tabs (\t) in your strings, e.g:

 Console.WriteLine("-|- Name: " + myList[i].GetName() + "\t | Surname: "
                     + myList[i].GetName() + "\t |");

Add as many tabs as you need.

Or you can ensure each column takes up at least x characters:

Console.WriteLine("-|- Name: {0,-x}| Surname: {1,-x}|", 
    myList[i].GetName(), 
    myList[i].GetSurname());

Or you can use the PadRight method on each column (set x to the number you want):

Console.WriteLine( ("-|- Name: " + myList[i].GetName()).PadRight(x,' ') + 
                  ("| Surname: " + myList[i].GetName() ) + "|".PadRight(x,' ');
tomsullivan1989
  • 2,760
  • 14
  • 21