I have an instance of StringBuilder in my C# application wherein I would like certain lines to be padded with a varying number of spaces depending on the context. My usage is very simple:
StringBuilder MyStringBuilder = new StringBuilder();
MyStringBuilder.AppendFormat("{0:" + this._padding + "}"); // <-- Exception thrown here
MyStringBuilder.AppendLine("My data.");
If this._padding==10, the resulting output should look something like this:
My data.
How can I pad my strings without resorting to using a for loop? Thanks.
ADDITIONAL INFO
Exception: “Index (zero based) must be greater than or equal to zero and less than the size of the argument list.”
{0,10}, not{0:10}.Try this:
There is also a
string.PadLeftmethod that you could use.