How can I align text in columns using Console.WriteLine?

前端 未结 8 1185
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 22:11

I have a sort of column display, but the end two column\'s seem to not be aligning correctly. This is the code I have at the moment:

Console.WriteLine(\"Cust         


        
相关标签:
8条回答
  • 2020-11-28 23:01

    You could use tabs instead of spaces between columns, and/or set maximum size for a column in format strings ...

    0 讨论(0)
  • 2020-11-28 23:10

    Just to add to roya's answer. In c# 6.0 you can now use string interpolation:

    Console.WriteLine($"{customer[DisplayPos],10}" +
                      $"{salesFigures[DisplayPos],10}" +
                      $"{feePayable[DisplayPos],10}" +
                      $"{seventyPercentValue,10}" +
                      $"{thirtyPercentValue,10}");
    

    This can actually be one line without all the extra dollars, I just think it makes it a bit easier to read like this.

    And you could also use a static import on System.Console, allowing you to do this:

    using static System.Console;
    
    WriteLine(/* write stuff */);
    
    0 讨论(0)
提交回复
热议问题