How To: Best way to draw table in console app (C#)

Lukas Šalkauskas picture Lukas Šalkauskas · May 13, 2009 · Viewed 140.6k times · Source

I have an interesting question. Imagine I have a lot of data changing in very fast intervals. I want to display that data as a table in console app. f.ex:

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------

How to keep things fast and how to fix column widths ? I know how to do that in java, but I don't how it's done in C#.

Answer

huusom picture huusom · May 13, 2009

Use String.Format with alignment values.

For example:

String.Format("|{0,5}|{1,5}|{2,5}|{3,5}|", arg0, arg1, arg2, arg3);

To create one formatted row.