Formatting a C# String with Identical Spacing in Between Values

Formatting a C# string with identical spacing in between values

You can use advanced features of string.Format:

string.Format("{0,-10}{1,-10}{2}", ...)

You can do the same thing by writing str.PadRight(10)

String interpolation and formatting a table

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.

Row-like string with even spacing in between values

First thing, I highly suggest using a pattern instead of concatenating your strings using plus sign. This will help you see things more clear:

string pattern = string.Format("{0}\t\t{1}\t\t{2}\t\t{3}\t\tAnnualSalary: {4}, Annual Bonus: {5}", 
emp[index].first,
emp[index].last,
emp[index].number,
emp[index].department,
emp[index].annualSalary).ToString("c"),
emp[index].annualBonus);

The answer to your question is that you are using tabs assuming they will fill the space for you, but they won't. You need to use advanced features of

string.Format or string.Pad

Full answer can be found here: Formatting a C# string with identical spacing in between values

String.Format()'s alignment behaves different when it comes to MessageBox

The String.Format(...) works perfectly well, but it does not align as a table in your message box because of the font!

In your console you use a fixed-width (monospace) font, but the messagebox uses one with dynamic width. Therefore e.g. an "M" consumes more space than an "i" or a " " (space) and this messes up your layout.

As you can't change the messagebox font, you have to create your own frame with any kind of text widget that supports custom fonts, e.g a TextBox or just an ordinary Label.

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.

String formatting with spaces

You can change your font in order to use a monospace font.

This way you will be able to use the String.Format and work with paddings.



Related Topics



Leave a reply



Submit