Reputation: 13
So I am fairly new to the "code world" and hopefully have a fairly simply question.
txtBox.Text = "x";
How would I make it so I am able to see 10 X's in my txtBox
without completely writing it out?
Upvotes: 1
Views: 61
Reputation: 16286
string
has a constructor that repeats a character for a given number of times:
txtBox.Text = new string('x', 10);
It's documented here.
If what you want to repeat is a string (not a single character) you need a loop, but use a StringBuilder
to minimise memory fragmentation. Try avoiding repetitive string concatenations:
var stringBuilder = new StringBuilder();
for(int i = 0; i < 10; i++)
stringBuilder.Append("ThePatternToRepeat");
txtBox.Text = stringBuilder.ToString();
Upvotes: 6
Reputation: 44288
string has a constructor for this :-
txtBox.Text = new string('x',10);
alternatives are
looping:
string s = "";
for (var n = 0; n < 10; n++) s += "x";
txtBox.Text = s;
Linq:
txtBox.Text = Enumerable.Range(0, 10).Aggregate("", (a, n) => a + "x");
Upvotes: 3