How to Fill String with Repeated Characters in C#?

Here’s a small tip on using the string class better in C# to fill a string with specified repeated characters.

You can use the string class constructor to fill a string that has all the repeated characters .

How to Fill String with Repeated Characters in C#?

For example, assume you want a string with 10 # or * symbols. one of doing this declaring a string like this

string str = “##########”;

The other easy and best way is to use the string class constructor like below

private void Form1_Load(object sender, EventArgs e)
{
     string stringSymbols = new string('#', 10);
     MessageBox.Show(stringSymbols);
}
How to Fill String with Repeated Characters in C#?

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...