Home > Mobile >  String interpolation and formatting a table
String interpolation and formatting a table

Time:02-23

TL:DR:

For C# string interpolation, where does the <alignment> actually add spaces in {<interpolationExpression>,<alignment>}? Is it the rightmost character of the preceding string? Is it the leftmost character of the preceding string? How can I make a simple table like the one shown below?

Toy Example:

I am trying to understand how string spacing works with string interpolation in C#. Consider the below table.

Score:  97
Name:   Jack
ID:     01123

I have tried to replicate this simple example below and have also put digits for tracking spacing between the 2nd column and the 1st column. I don't have double digits for the spacing string, rather it is 1 through 9 followed by a 0 that represents 10. Then it is 1 through 9 again (representing 11-19 spaces) followed by another 0 (representing 20 spaces).

Console.WriteLine("12345678901234567890"); // For tracking spacing
Console.WriteLine($"Score: {97, 10}");
Console.WriteLine($"Name: {"Jack", 10}");
Console.WriteLine($"ID: {01123, 10}");

the output of which is

12345678901234567890
Score:         97
Name:       Jack
ID:       1123

The spacing for the line Score: ... 97 indicates that the spacing between 97 and Score: is based on the last character of Score:, which is :. I draw this conclusion based on the spacing string 6 at the : location and the spacing string 6 at the 9 location. However, the spacing for the line Name: ... Jack, the distance between the : in Name and J in Jack is 12.

I think one solution to deal with this alignment issue might be to use the String.Length property, but I am not sure. How does alignment work in C# string interpolation, and how can I use it to make a simple table like I have shown?

CodePudding user response:

The problem is the format still makes each value string the same length, but the initial field names are not the same length, and so your fixed-length values strings start from different places.

For this case, where the first column is static text and the second column is always left-justified, the extra format is not helpful because you can put in the spaces directly:

Console.WriteLine("12345678901234567890"); // For tracking spacing
Console.WriteLine($"Score:  {97}");
Console.WriteLine($"Name:   {"Jack"}");
Console.WriteLine($"ID:     {01123}");

But let's say you have data more like this:

var items = new List<(string, object)>  {
    ("Score", 97),
    ("Name", "Jack")
    ("ID",   1123)
};

You can handle that like by putting the padding on the leading field names, like so:

foreach(var item in items)
{
    Console.WriteLine($"{item.Item1,-10}{item.Item2}");
}

But this is still missing the : separator. You could restore it like this:

foreach(var item in items)
{
    Console.WriteLine($"{item.Item1 ':',-10}{item.Item2}");
}

The other thing potentially worth exploring here is a tab character, though that can be tricky if the variance between your field names is more than the width of the tab.

  • Related