Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a set of characters

Tags:

string

c#

.net

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!

like image 754
Greg McGuffey Avatar asked Sep 29 '11 17:09

Greg McGuffey


People also ask

How do you repeat a character in Excel?

To repeat a character in a cell, use the REPT function.

How do you repeat a char in Java?

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.

How do you find a repeating character?

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.


2 Answers

A slight variation on the answer by Bala R

var s = String.Concat(Enumerable.Repeat("-.", 10));
like image 66
wageoghe Avatar answered Oct 19 '22 01:10

wageoghe


var result = String.Join("", Enumerable.Repeat("-.", 10));
like image 20
Bala R Avatar answered Oct 19 '22 03:10

Bala R