I was surprised to find that the string class in C# does not have a Replicate() method.
The fastest way (in terms of lines of code) to replicate a given string seems to be a combination of string.Concat() and ArrayList.Repeat()
string.Concat(System.Collections.ArrayList.Repeat(SourceString, NumberOfRepetitions).ToArray());
For example, if you want to indicate that a password has been provided, but should not be displayed (obviously!), use the line
LabelPassword.Text = string.Concat( System.Collections.ArrayList.Repeat('*', LabelPassword.Text.Length).ToArray());
in the DataBinding event of the password label control.
Wouldn’t string s = new string(‘*’, count); do the job a little more clearly?
Thank you Chris,
that was *what* I was looking for, but not *where* I was looking. As you can see from my entry, I expected a method or a static method.
I checked now why I did not find this information myself. After pressing F1 on the “string” keyword in VS, you need FOUR clicks to get to the string() constructor, and only if you know where you want to end up!
Also the string constructor only allows a char parameter (which is sufficient for the current example), but no string.
Found this at: http://forums.asp.net/t/973717.aspx
new System.Text.StringBuilder().Insert(0,”myString”,count).ToString()
Worked like a charm for me.
Pingback: Replicate String in C# « devioblog
thanks,,,