Writers logo

String Repetition in C# with the new string() Constructor

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

By Sukhpinder SinghPublished about a year ago • 1 min read

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!

ChallengeCommunityInterviewsPromptsVocalWriter's BlockWriting ExerciseGuides

About the Creator

Sukhpinder Singh

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.