How to do Alignment within string.Format in C#?

You can do something like this:

Console.WriteLine(String.Format("{0,-10} | {1,5}", "Bill", 51));

You’ll get “51” aligned to right on 5 characters.

More examples here: Align String with Spaces.

For official reference, see Composite Formatting

Leave a Comment