C# String builder, displaying data nicely spaced out.

Posted by Sef on Stack Overflow See other posts from Stack Overflow or by Sef
Published on 2010-04-19T10:45:03Z Indexed on 2010/04/19 10:53 UTC
Read the original article Hit count: 156

Filed under:

Hello,

I am wondering how exactly can i order my date nicely in a stringbuilder?
Meaning something equal to ("{0,2}", ....) to space the data nicely out.
I do not want to use consolewrites of any kind, so the class can be re-used in a form, console code etc...

Currently i am using " " for the spacing, but in overall it does not give a proper display. (it messed up when i have numbers with more then 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*/

Regards.

© Stack Overflow or respective owner

Related posts about c#