Python Vs C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python Vs C#

How can I duplicate a string n times in c# I know with python it something like: string * n. But this is not applicable in c#. How do I do it?

24th Feb 2020, 4:02 PM
Marvellous Abia
Marvellous Abia - avatar
5 Answers
+ 1
You have to use loops to achieve that
24th Feb 2020, 4:05 PM
Arsenic
Arsenic - avatar
+ 1
Found a solution here: https://stackoverflow.com/questions/411752/best-way-to-repeat-a-character-in-c-sharp This looked the easiest: Console.WriteLine(string.Concat(Enumerable.Repeat("a", 22))); The Console.WriteLine was added by me to test it.
24th Feb 2020, 6:50 PM
Paul
Paul - avatar
0
Is that the best or only approach?
24th Feb 2020, 4:08 PM
Marvellous Abia
Marvellous Abia - avatar
0
In the c we can call strndup (a, b, n) to duplicate a to b n char, c++ either, i dont know in C#
24th Feb 2020, 4:56 PM
Tegar
Tegar - avatar
0
I believe C# has a builtin method to perform string duplication, but if you aren't able to use it, you can define a function to do it using loops.
24th Feb 2020, 6:10 PM
Seb TheS
Seb TheS - avatar