Search code examples
c#splitconsole-applicationconsole.writeline

c# Splitting a string into columns using console


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


Solution

  • 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, ' '));