'String interpolation and formatting a table

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source