In Python, you can write 2*'hello'
in the shell and the IDLE will output 'hellohello'
. Is there a way to output 'multiples' of a string in C# to do the same thing?
Asked
Active
Viewed 115 times
-3

Prune
- 76,765
- 14
- 60
- 81

Thomas Stokes
- 97
- 10
-
just use a for loop, haven't heard of it beeing as simple as in python – KH241 Feb 06 '21 at 01:09
-
Unfortunately you can do it e.g. in foreach loop – zolty13 Feb 06 '21 at 01:10
-
Does this answer your question? [Is there an easy way to return a string repeated X number of times?](https://stackoverflow.com/questions/3754582/is-there-an-easy-way-to-return-a-string-repeated-x-number-of-times) – Peter Duniho Feb 06 '21 at 01:15
-
Does this answer your question? [Is there a built-in function to repeat a string or char in .NET?](https://stackoverflow.com/questions/3754582/is-there-an-easy-way-to-return-a-string-repeated-x-number-of-times) – Peter Duniho Feb 06 '21 at 01:17
1 Answers
0
You can use Enumerable.Repeat
Generates a sequence that contains one repeated value.
Then String.Concat
Concatenates one or more instances of String, or the String representations of the values of one or more instances of Object.
Example
Console.WriteLine(string.Concat(Enumerable.Repeat("hello",3)));
Output
hellohellohello

TheGeneral
- 79,002
- 9
- 103
- 141