The easiest way to build a string if I have a character and length

In C #, as inputs I:

Letter: "A"
Length: 5

and I want to output:

"AAAAA"

Is there a more elegant way to do this other than a loop?

+5
source share
3 answers

You can use the string constructor: new string('A', 5);

+8
source

You can simply create a character that repeats the string constructor to create a string.

Console.WriteLine(new String('A', 5));
+2
source

Yes:

String st = new String('A', 5);

See MSDN .

+2
source

All Articles