String interpolation with alignment

String interpolation provides a mechanism to align (and pad) interpolated values

Home DailyDrop

Daily Knowledge Drop

When using string interpolation ($), the interpolation expressions results (the final string resolved into the main string) can be left or right aligned, including padding to be used when aligning.

This can be very useful when having interpolation expressions which result in string of varying lengths - having these values alignment can result in a more uniform output.


Examples

In all the below examples, a collection of sale values of varying lengths is used:

var saleValues = new[]
{
    100,
    54500,
    1,
    8514,
    -500
};

Default

If we want to output each of them without any alignment:

The sale amount is: 100 (for the month of August)
The sale amount is: 54500 (for the month of August)
The sale amount is: 1 (for the month of August)
The sale amount is: 8514 (for the month of August)
The sale amount is: -500 (for the month of August)

Right align

If we require a more uniform output, a positive number can be used to right align and pad the value:

foreach (var sale in saleValues)
{
    Console.WriteLine($"The sale amount is: {sale, 9} (for the month of August)");
}

The above will right align the sale value and make it a uniform 9 characters in length:

The sale amount is:       100 (for the month of August)
The sale amount is:     54500 (for the month of August)
The sale amount is:         1 (for the month of August)
The sale amount is:      8514 (for the month of August)
The sale amount is:      -500 (for the month of August)

Left align

To left align, a negative number is specified:

foreach (var sale in saleValues)
{
    Console.WriteLine($"The sale amount is: {sale, -9} (for the month of August)");
}

The above will left align the sale value and make it a uniform 9 characters in length:

The sale amount is: 100       (for the month of August)
The sale amount is: 54500     (for the month of August)
The sale amount is: 1         (for the month of August)
The sale amount is: 8514      (for the month of August)
The sale amount is: -500      (for the month of August)

Const value

The value specified is required to be a constant value.

So this is valid:

// must be a constant
const int length = 10;

foreach (var sale in saleValues)
{
    Console.WriteLine($"The sale amount is: {sale, length} (for the month of August)");
}

However, this is NOT VALID, as length is not a const:

int length = saleValues.Length;

foreach (var sale in saleValues)
{
    Console.WriteLine($"The sale amount is: {sale, length} (for the month of August)");
}

Notes

A small lesser-known feature of string interpolation, however it can be very useful in producing uniform output when required, with very little additional effort.


References

Khalid Abuhakmeh Tweet


Daily Drop 130: 03-08-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net string interpolation alignment