Repeat String X Times in C#

  1. Repeat String X Times With the string Class Constructor in C
  2. Repeat String X Times With the StringBuilder Class in C
  3. Repeat String X Times With the LINQ Method in C

This tutorial will introduce the methods to repeat a string x times in C#.

Repeat String X Times With the string Class Constructor in C

The constructor of the string class can be used to repeat a specific character to a specified number of times inside a string in C#. We can pass the character to be repeated and the number of times it should be repeated to the constructor of the string class in C#. The string(c, x) constructor gives us a string where the character c is repeated x times. See the following code example.

using System;

namespace repeat_string_x_times
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = new string('e', 3);
            Console.WriteLine(str);
        }
    }
}

Output:

eee

In the above code, we repeated the character e three times and saved it inside the string variable str with the string('e', 3) constructor in C#. This method can only be used to repeat a single character x times inside a string.

Repeat String X Times With the StringBuilder Class in C

The StringBuilder class can also be used to repeat a string x times in C#. The StringBuilder class creates a mutable string of characters of a certain length in C#. We can then use the StringBuilder.Insert(s,x) function to insert the string s and repeat it x times. See the following code example.

using System;
using System.Text;

namespace repeat_string_x_times
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = new StringBuilder("TEXT".Length * 3).Insert(0, "TEXT", 3).ToString();
            Console.WriteLine(str);
        }
    }
}

Output:

TEXTTEXTTEXT

In the above code, we repeated the string TEXT 3 times and saved it inside the string variable str with the StringBuilder class in C#. We first created a mutable string that has a combined length of TEXT.Length * 3 characters. We then started from the index 0 and inserted the string TEXT 3 times inside the mutable string. We then converted the mutable string to a regular string with the ToString() function in C#. This approach can be used to repeat strings rather than characters in the previous approach.

Repeat String X Times With the LINQ Method in C

The LINQ, or else known as the language integrated query, is used to integrate the SQL query functionality with data structures in C#. We can use the Enumerable.Repeat() function of LINQ to repeat a string x number of times in C#. The Enumerable.Repeat(s, x) function takes two parameters, the string variable s and the integer variable x, the number of times that string variable must be repeated. See the following code example.

using System;
using System.Linq;

namespace repeat_string_x_times
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = string.Concat(Enumerable.Repeat("TEXT", 3));
            Console.WriteLine(str);
        }
    }
}

Output:

TEXTTEXTTEXT

In the above code, we repeated the string TEXT 3 times and saved it in the string variable str with the Enumerable.Repeat("TEXT", 3) function of LINQ in C#. We used the Enumerable.Repeat() function to repeat the string and the String.Concat() function to concatenate those values into a string. This approach is recommended because it does the same job as the previous approach and is relatively simple.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp String

  • Check Palindrome String in C#
  • Case Insensitive Contains String Function in C#