C # String builder displaying data well distributed

I'm wondering how exactly can I order my date in a string device?
The value is something equal ("{0,2}", ....) to easily highlight the data.
I don't want to use console entries of any type, so the class can be reused in the form, console code, etc.

I am currently using "" for spacing, but overall it does not render correctly. (it messed up when I have digits with more than 1 digit)

public override string ToString() 
    {
        StringBuilder builder = new StringBuilder();
        foreach (int value in tabel)
        {
            builder.Append(value); // should have something similiar to ("{0,2}", ....)
            builder.Append("  "); // should have something similiar to ("{0,2}", ....)
        }

        builder.Append("(top:");
        builder.Append(top);
        builder.Append(")");

        return builder.ToString();
    }/*ToString*/

      

Sincerely.

+2


a source to share


2 answers


Use AppendFormat like:

builder.AppendFormat("{0}  ", value);

      



or

builder.AppendFormat("{0}  {1}  {2}", value.property1, value.property2, value.property3);

      

+5


a source


Use string formatting. This example will fit 3 spaces for each number, even for numbers with a lower digit.



value.ToString("000");

      

+1


a source







All Articles