Format a string into columns

后端 未结 2 2067
别跟我提以往
别跟我提以往 2020-11-29 04:35

Is there a cool way to take something like this:

Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653
2条回答
  •  误落风尘
    2020-11-29 04:55

    In addition to Tomas's answer I just want to point out that string interpolation can be used in C# 6 or newer.

    // with string format
    var columnHeaders1 = string.Format($"|{0,-30}|{1,-4}|{2,-15}|{3,-30}|{4,-30}|{5,-30}|{6,-30}", "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG");
    
    // with string interpolation
    var columnHeaders2 = $"|{"ColumnA",-30}|{"ColumnB",-4}|{"ColumnC",-15}|{"ColumnD",-30}|{"ColumnE",-30}|{"ColumnF",-30}|{"ColumnG",-30}";
    

提交回复
热议问题