2

I have a message where I print out the Name of the Entity and Its Count. I have used StringBuilder class. I have code like this:

   message.AppendFormat("Items1:    {0}",item1count).AppendLine();
   message.AppendFormat("Items2Byb:    {0}",item2count).AppendLine();
   message.AppendFormat("Items3STCDEE:    {0}",item3count).AppendLine();
   message.AppendFormat("Items4HTECEEGG:    {0}",item4count).AppendLine();
   message.AppendFormat("ItemsASSTEC:    {0}",item5count).AppendLine();

The result is coming out like this:

  Items1:         1
  Items2Byb:         3
  Items3STCDEE           5

What I want is the proper alignment, no matter what the length of the Item name is:

      Items1:            1
      Items2Byb:         3
      Items3STCDEE       5
3

2 Answers 2

6

Here is an approach without using PadRight which assumes that the maximum length of the label on the left is 20. Naturally, you should adjust the number up to be 1 + max.

using System;
using System.IO;
using System.Text;

public class P{
    public static void Main(string[] args) {
        StringBuilder message = new StringBuilder();
        message.AppendFormat("{0,-20}{1}","Items1:",1).AppendLine();
        message.AppendFormat("{0,-20}{1}","Items2Byb:",2).AppendLine();
        message.AppendFormat("{0,-20}{1}","Items3STCDEE:", 3).AppendLine();
        message.AppendFormat("{0,-20}{1}","Items4HTECEEGG:",4).AppendLine();
        message.AppendFormat("{0,-20}{1}","ItemsASSTEC:",5).AppendLine();
        Console.WriteLine(message.ToString());
    }
}

Output:

Items1:             1
Items2Byb:          2
Items3STCDEE:       3
Items4HTECEEGG:     4
ItemsASSTEC:        5
Sign up to request clarification or add additional context in comments.

3 Comments

I am not getting the respective results, it's just pushing the numbers to the right. I don't know why?
@c_sharp are you using exactly the format string above with no extra spaces?
@C_sharp, Also the number you need might be larger than 20, depending on how long your longest label string is.
4

For simple formatting you can use PadRight (using literal 1,3,4 for simplicity)

var message = new StringBuilder();
int width = 20;

message.Append("Items1:".PadRight(width) + 1).AppendLine();
message.Append("Items2Byb:".PadRight(width) + 3).AppendLine();
message.Append("Items3STCDEE:".PadRight(width) + 4).AppendLine();
message.Append("Items4HTECEEGG:".PadRight(width) + 5).AppendLine();
message.Append("ItemsASSTEC:".PadRight(width) + 6).AppendLine();

will print:

Items1:             1
Items2Byb:          3
Items3STCDEE:       4
Items4HTECEEGG:     5
ItemsASSTEC:        6

Or you can define custom method (to know more about {0,-20} format consider this article. Essentially it stated to pad white-spaces from right until the string occupy 20 characters long. ):

public void AddLine(StringBuilder builder, string name, int val)
{
    builder.AppendFormat("{0,-20}{1}", name, val).AppendLine();
}

and reuse it like:

AddLine(message, "Items1:", 1);
AddLine(message, "Items2Byb:", 3);
AddLine(message, "Items3STCDEE:", 4);
AddLine(message, "Items4HTECEEGG:", 5);
AddLine(message, "ItemsASSTEC:", 6);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.