Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Create a blank string of x length?

Hi
Not sure how much sense this makes.
But can you create a blank string of x length?
i.e. if
int x = 2;
mystring = "  ";
thanks
Paul
0
paulwhelan
Asked:
paulwhelan
  • 2
1 Solution
 
osmantemizCommented:
Here:

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

0
 
PoeticAudioCommented:
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

0
 
Carl TawnSystems and Integration DeveloperCommented:
You simply use the constructor for string that takes a char and a int:

    string s = new string(' ', 10);
0
 
PoeticAudioCommented:
even better!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now