C# Tips and Tricks #19 – Repeat a character X times

Here’s a quick one. Its rather a simple tip demonstrating how to repeat a character X times in C#.

How to repeat a character X times in C# ?

Imagine that you want to repeat a character “X” , 5 times. One of the simplest way to do it is using the constructor of the string class and passing the character(X) and 5 as its parameters as shown below.

using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string tabs = new String('X', 5);
            Console.WriteLine(tabs);

            Console.ReadLine();
        }
    }
}
    

This will display the character X , 5 times as shown below.

How to repeat a character X times in C# ?

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...