Create a blank string of x length?

Posted on 2006-05-12
Last Modified: 2010-04-16
Not sure how much sense this makes.
But can you create a blank string of x length?
i.e. if
int x = 2;
mystring = "  ";
Question by:paulwhelan
    LVL 3

    Accepted Solution


    int x = 2
    string mystring="";
    for (int i=0;i<x;i++)
        mystring += " ";

    LVL 6

    Expert Comment

    You could also make a cute little method like below (osmantemiz may perform faster though, not sure, this is just an alternative approach)

    private string CreateBlankString(int numBlanks)
        string str = "";
        str = str.PadLeft(numBlanks, ' ');

        return str;

    then you could use it like
    string blankStr = CreateBlankString(10);  //create a string with 10 blanks

    LVL 52

    Expert Comment

    by:Carl Tawn
    You simply use the constructor for string that takes a char and a int:

        string s = new string(' ', 10);
    LVL 6

    Expert Comment

    even better!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now