I'd like to repeat a set of characters multiple times. I know how to do it with a single character:
string line = new string('x', 10);
But what I'd like would be something more like this:
string line = new string("-.", 10);
which would result in: -.-.-.-.-.-.-.-.-.-.
I know the string constructor can't do it, but is there some other way within the BCL? Other suggestions?
Thanks!
To repeat a character in a cell, use the REPT function.
Here is the shortest version (Java 1.5+ required): repeated = new String(new char[n]). replace("\0", s); Where n is the number of times you want to repeat the string and s is the string to repeat.
To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.
A slight variation on the answer by Bala R
var s = String.Concat(Enumerable.Repeat("-.", 10));
var result = String.Join("", Enumerable.Repeat("-.", 10));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With