How to return a string repeated N number of times in C#?


Use string instance string repeatedString = new string(charToRepeat, 5) to repeat the character "!" with specified number of times.

Use string.Concat(Enumerable.Repeat(charToRepeat, 5)) to repeat the character "!" with specified number of times.

Use StringBuilder builder = new StringBuilder(stringToRepeat.Length * 5); to repeat the character "!" with specified number of times.

Using string instance

Example

 Live Demo

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 Number: {myString}{repeatedString}");
         Console.ReadLine();
      }
   }
}

Output

String: Hi
Character to repeat: !
Repeated String: Hi!!!!!

In the above example using string instance string repeatedString = new string(charToRepeat, 5) we are specifying the character "!" should repeat the specified number of times.

Using string.Concat and Enumberable.Repeat −

Example

 Live Demo

using System;
using System.Linq;
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}");
         var repeatedString = string.Concat(Enumerable.Repeat(charToRepeat, 5));
         Console.WriteLine($"Repeated String: {myString}{repeatedString}");
         Console.ReadLine();
      }
   }
}

Output

String: Hi
Character to repeat: !
Repeated String: Hi!!!!!

In the above example using string instance string.Concat(Enumerable.Repeat(charToRepeat, 5)) we are repeating the character "!" with specified number of times.

Using StringBuilder

Example

 Live Demo

using System;
using System.Text;
namespace DemoApplication{
   public class Program{
      static void Main(string[] args){
         string myString = "Hi";
         Console.WriteLine($"String: {myString}");
         string stringToRepeat = "!";
         Console.WriteLine($"String to repeat: {stringToRepeat}");
         StringBuilder builder = new StringBuilder(stringToRepeat.Length * 5);
         for (int i = 0; i < 5; i++){
            builder.Append(stringToRepeat);
         }
         string repeatedString = builder.ToString();
         Console.WriteLine($"Repeated String: {myString}{repeatedString}");
         Console.ReadLine();
      }
   }
}

Output

String: Hi
Character to repeat: !
Repeated String: Hi!!!!!

In the above example using string builder we are getting the length of the string to be repeated. Then in the for loop we are appending the string "!" with specified number of times.

Updated on: 24-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements