Formatting a string into columns using String Interpolation

You can use Alignment Component for this purpose. Like this:

Console.WriteLine($"value: {d,-17} {s}");

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

So this is why we use negative alignment because you want the first column be left-aligned.

Leave a Comment