String Repetition in C# with the new string() Constructor
Discover the power of the new string() constructor for efficient string repetition in C#.

Introduction
Often developers are required to repeat certain characters especially while dealing with string formatting and formulating text outputs.
The C# programming language is a straightforward approach to repeating a certain character a specified number of times.
The feature is particularly useful while working with
- Indentation
- Format Documents
- Code Block Styling
Getting Started
The new string() constructor allows you to create a new string by specifying the character a certain number of times.
Basic Syntax
string repeatedString = new string(char character, int repeatTimes);
where,
- character is the character that needs to be repeated.
- repeatTimes is the number of time the above character need to be repeated.
Example
Consider an example where the user needs to add a certain tab space between two string texts.
int n = 3; // The number of tabs
string tabs = new string('\t', n);
Console.WriteLine($"Start of text: {tabs} Indented text");
Console Output
Start of text: Indented text
C# Programming🚀
Thank you for being a part of the C# community!



Comments
There are no comments for this story
Be the first to respond and start the conversation.