Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to return a string repeated N number of times in C#?
There are several ways to repeat a string or character N number of times in C#. This article covers three effective approaches: using the string constructor for characters, string.Concat with Enumerable.Repeat, and StringBuilder for more complex scenarios.
Using String Constructor for Characters
The simplest way to repeat a single character is using the string constructor that takes a character and a count −
string repeatedString = new string(character, count);
Example
using System;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
string myString = "Hi";
Console.WriteLine($"String: {myString}");
char charToRepeat = '!';
Console.WriteLine($"Character to repeat: {charToRepeat}");
string repeatedString = new string(charToRepeat, 5);
Console.WriteLine($"Repeated String: {myString}{repeatedString}");
}
}
}
The output of the above code is −
String: Hi Character to repeat: ! Repeated String: Hi!!!!!
Using string.Concat and Enumerable.Repeat
This approach works for both characters and strings, providing more flexibility than the string constructor −
string repeatedString = string.Concat(Enumerable.Repeat(item, count));
Example with Character
using System;
using System.Linq;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
string myString = "Hello";
Console.WriteLine($"Original String: {myString}");
char charToRepeat = '*';
var repeatedChar = string.Concat(Enumerable.Repeat(charToRepeat, 3));
Console.WriteLine($"Repeated Character: {repeatedChar}");
string stringToRepeat = "ABC";
var repeatedString = string.Concat(Enumerable.Repeat(stringToRepeat, 4));
Console.WriteLine($"Repeated String: {repeatedString}");
}
}
}
The output of the above code is −
Original String: Hello Repeated Character: *** Repeated String: ABCABCABCABC
Using StringBuilder
For better performance when dealing with large strings or many repetitions, StringBuilder is the preferred approach −
StringBuilder builder = new StringBuilder(capacity);
for (int i = 0; i < count; i++) {
builder.Append(stringToRepeat);
}
string result = builder.ToString();
Example
using System;
using System.Text;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
string baseString = "Code";
Console.WriteLine($"Base String: {baseString}");
string stringToRepeat = "->";
Console.WriteLine($"String to repeat: {stringToRepeat}");
StringBuilder builder = new StringBuilder(stringToRepeat.Length * 6);
for (int i = 0; i < 6; i++) {
builder.Append(stringToRepeat);
}
string repeatedString = builder.ToString();
Console.WriteLine($"Final Result: {baseString}{repeatedString}");
}
}
}
The output of the above code is −
Base String: Code String to repeat: -> Final Result: Code->->->->->->
Performance Comparison
| Method | Best For | Performance |
|---|---|---|
| String Constructor | Single characters only | Fastest for characters |
| string.Concat + Enumerable.Repeat | Small to medium repetitions | Good readability |
| StringBuilder | Large strings or many repetitions | Best for performance-critical scenarios |
Conclusion
Use the string constructor for repeating single characters, string.Concat with Enumerable.Repeat for flexibility with both strings and characters, and StringBuilder when performance is critical or dealing with large repetitions. Choose the method that best fits your specific use case.
