-1

I have to get strings out of a text file and save them to a list. For each line I should write down the outcomes written in columns perfectly sorted under each other.

I have to write every line given in the text file down in the console.

I have a string build out of multiple strings like this what represents 1 line:

string uitslag = vertrek.ToString() +  van.ToString()  + naar.ToString() + status.ToString() + vliegtuig.ToString();`

What my question was: how can I sort the output like this:

aaaaaa    aaaaaa     aa       aaaaa       aaaaa
aaaa      aaa        aaaa     aa          aa
a         a          a        a           a
aaaaaaa   a          aaaa     aa          aaaaaaaa`

Note: I already tried using \t but then some lines wont be in sync with the others.

Edit:

`String.Format("{0}{1}{2}{3}{4}",
      vertrek.ToString().PadRight(12, ' '),
      van.ToString().PadRight(12, ' '),
      naar.ToString().PadRight(12, ' '),
      status.ToString().PadRight(12, ' '),
      vliegtuig.ToString().PadRight(12, ' '));`

gave the following result:

result

2 Answers2

3

You can use String.PadRight with the maximum length as a space padding to the right of each string:

String.PadRight(50, ' ');

In your case:

string uitslag = String.Format("{0}{1}{2}{3}{4}",    
         vertrek.ToString().PadRight(50, ' '),  
         van.ToString().PadRight(50, ' '),  
         naar.ToString().PadRight(50, ' '), 
         status.ToString().PadRight(50, ' '),                  
         vliegtuig.ToString().PadRight(50, ' '));
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • What was the outcome ? – Koby Douek Mar 08 '17 at 17:37
  • 18-11-2014 06:48:00(LWD) Leeuwarden Int(RAK) MarrakechOnderweg KL4124KLMBoeing 737-800 (winglets) Passenger 18-11-2014 06:48:00(LWD) Leeuwarden Int(RAK) MarrakechOnderweg KL4124KLMBoeing 737-800 (winglets) Passenger 18-11-2014 06:31:00(LWC) Leeuwarden City(ACE) LanzaroteOnderweg TV2322TransaviaBoeing 727-300 (winglets) Passenger 18-11-2014 06:45:00(LWC) Leeuwarden City(BRU) BrusselsGeland KL3231KLMFokker 70 18-11-2014 06:45:00(LWS) Leeuwarden City(BRU) BrusselsGeland KL3231KLMFokker 70 18-11-2014 06:45:00(LWS) Leeuwarden City(BRU) BrusselsGeland KL3231KLMFokker 7 – arlan schouwstra Mar 08 '17 at 17:37
  • there are no spaces whatsoever – arlan schouwstra Mar 08 '17 at 17:38
  • Increase the 12 to the maximus size like I wrote - instead of 12, try 50 – Koby Douek Mar 08 '17 at 17:38
1

Change this:

string uitslag = vertrek.ToString() +  van.ToString()  + naar.ToString() + status.ToString() + vliegtuig.ToString();

Into something like this:

string uitslag = String.Format ("{0,-10}{1,-10}{2,-10}{3,-10}{4,-10}", vertrek.ToString(),  van.ToString(), naar.ToString(), status.ToString(), vliegtuig.ToString());

The first number in curly brackets represents the column number, the second represents the maximum number of characters in that line (negative value for left align, positive value for right align). So you'll want to set that value (I used 10 as an example) to whatever your maximum column width should be.

C. Helling
  • 1,394
  • 6
  • 20
  • 34