How can I add certain number (between 1 and 100) of whitespaces to StringBuilder?
StringBuilder nextLine = new StringBuilder();
string time = Util.CurrentTime;
nextLine.Append(time);
nextLine.Append(/* add (100 - time.Length) whitespaces */);
What would be “ideal” solution? for loop is ugly. I can also create array where whitespaces[i] contains string which contains exactly i whitespaces, but that would be pretty long hardcoded array.
You can use the
StringBuilder.Append(char,int)method, which repeats the specified Unicode character a specified number of times:Better yet, combine both appends into a single operation:
This would append your
timestring, followed by100 - time.Lengthspaces.Edit: If you’re only using the
StringBuilderto construct the padded time, then you can do away with it altogether: