0

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?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Jared Frazier
  • 413
  • 1
  • 4
  • 10
  • 2
    Form the [docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated): alignment - The constant expression whose value defines the minimum number of characters in the string representation of the expression result. If positive, the string representation is right-aligned; if negative, it's left-aligned. For more information, see Alignment Component. – phuzi Feb 22 '22 at 15:22
  • 2
    Seems that you need to pad the labels, not the values. – phuzi Feb 22 '22 at 15:23
  • 1
    does this help [How can I align text in columns using Console.WriteLine?](https://stackoverflow.com/questions/4449021/how-can-i-align-text-in-columns-using-console-writeline) – Lei Yang Feb 22 '22 at 15:24
  • 1
    @phuzi yes, you were right, my problem was that I was padding the value and not the label. Thank you for the docs link, I looked there but didn't read closely enough! Also, the alignment should be negative, and not positive for what I want. – Jared Frazier Feb 22 '22 at 15:32

1 Answers1

1

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794