Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace whole characters in a string as same characters in c#?

Tags:

string

c#

Example:

string input = "super";
string rep = "a";

I want the output same charterers as per given input string length. The output should be "aaaaa". I dont like to use own FOR or While loops logic, is there any alternatives to accomplish it.

like image 627
Thiru G Avatar asked Jun 01 '13 07:06

Thiru G


1 Answers

Use the constructor

string output = new string('a', input.Length);

If you want to repeat a real string n-times, you could use Enumerable.Repeat:

string output = string.Join("", Enumerable.Repeat(rep, input.Length));

I use String.Join to concatenate each string with a specified seperator(in this case none).

like image 78
Tim Schmelter Avatar answered Oct 13 '22 01:10

Tim Schmelter