Best way to repeat a character in a string C#
Hello everyone! I'm new to C# and came across this task: I need to generate a string consisting of a repeating character. For example, I need to create a string of 10 characters "A" — "AAAAAAAAAA". \n\nCurrently I'm doing it with a loop:\n\n```csharp\nstring result = "";\nfor (int i = 0; i < 10; i++)\n{\n result += "A";\n}\n```\n\nBut I think this is not very efficient — strings in C# are immutable, and each concatenation creates a new object. For large repetitions, this probably heavily loads memory and CPU.\n\nI know there is `StringBuilder`, but maybe there is an even simpler or more performant way? In other languages, for example in Python, you can write `"A" * 10`, and that's it. Is there something similar in C#?\n\nI want to understand:\n1. Which method is the fastest?\n2. Which method is the most
No answers yet.
Log in to leave an answer
Blogs
Functional Programming in JavaScript: Basics and Examples