hedgesketch
hedgesketch

Reputation: 197

C# Console columns formatting and positioning

I've got a couple of questions about column formatting in a console app. My code and output is below.

  1. Why is there is a space before the first asdf?
  2. Why does the guid not line up like the other rows?

3.How come when I specify {0,5} it starts printing after the 7th character column?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person { Id = Guid.NewGuid(), Name = "John", Email = "[email protected]", Phone = "123456789" };
            Console.WriteLine("{0} DETAILS", person.Name.ToUpper());
            Console.WriteLine("{0,5} {1,5}", "asfd", "asdf");
            Console.WriteLine("Id: {0,5}", person.Id.ToString());
            Console.WriteLine("Name: {0,5}", person.Name);
            Console.WriteLine("Phone: {0,5}", person.Phone);
            Console.WriteLine("Email: {0,5}", person.Email);
            Console.ReadKey();
        }

        public class Person
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public string Phone { get; set; }
        }
    }
}

This my console output:

JOHN DETAILS
 asfd  asdf
Id: 21257c7f-dae0-4876-91bb-0fe88d926500
Name:  John
Phone: 123456789
Email: [email protected]

Upvotes: 3

Views: 1364

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

Why is there is a space before the first asdf?

When you do {0,5} what the 5 represents is "Right justify text 5 spaces in starting at the point where { is". So when you do "asdf" that becomes " asdf"

Why does the guid not line up like the other rows?

Because when you go over the number you specified it starts left justifying the text so because the text is so long adding the ,5 does nothing.

How come when I specify {0,5} it starts printing after the 7th character column?

Because the ,5 does not know nor care what text came before it. It only does offsets based on where you placed the placeholder.

See Reticulated answer for a code example of how to do proper aligning.

Upvotes: 2

Reticulated Spline
Reticulated Spline

Reputation: 2012

Why is there is a space before the first asdf?

Because the ,5 in {0,5} means "align to 5 characters", and "asfd" is only 4 characters, so a space is used for padding.

Why does the guid not line up like the other rows?

You're trying to align the entire ID which is more than 5 columns, and that many characters can't "fit" into 5 columns.

How come when I specify {0,5} it starts printing after the 7th character column?

Where do you see that happening?

I assume what you want is to align everything up, so you can do this:

Console.WriteLine("{0,5}: {1}", "Id", person.Id.ToString());
Console.WriteLine("{0,5}: {1}", "Name", person.Name);
Console.WriteLine("{0,5}: {1}", "Phone", person.Phone);
Console.WriteLine("{0,5}: {1}", "Email", person.Email);

This will align all the field names together. This works because the longest field name is 5 characters long. You can change the ,5 to something else, and if they're all the same (and longer than 5) then all your field names will right-align by being padded with spaces on the left.

You can read more about it on MSDN here: http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.110%29.aspx

Upvotes: 4

Related Questions