C# String builder, displaying data nicely spaced out
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.
Use AppendFormat, eg:
builder.AppendFormat("{0} ", value);
or
builder.AppendFormat("{0} {1} {2}", value.property1, value.property2, value.property3);
Use string formatting. This example will put 3 places for each number, even for less-digit numbers.
value.ToString("000");
精彩评论