Create string of length X

  • Thread starter Thread starter Martin Rosén-Lidholm
  • Start date Start date
M

Martin Rosén-Lidholm

When unit testing constraints of my business object, I oftentimes need
strings of a certain length. What characters are of no importance.

I'm looking for an elegant way of doing this.

Regards,
// Martin Rosén-Lidholm
 
Martin Rosén-Lidholm said:
When unit testing constraints of my business object, I oftentimes need
strings of a certain length. What characters are of no importance.

I'm looking for an elegant way of doing this.

Regards,
// Martin Rosén-Lidholm
How about padding?
 
string s = new string('#', 20); // gives "####################"

Chris.
 
Hmmm, how about:

class Builder
{
static string MakeString(int iLength)
{
StringBuilder sb = new StringBuilder();

for(int i = 0; i < iLength; i++)
{
sb.Append('x');
}

return sb.ToString();
}
}

so, string str = Builder.MakeString(5); gives you: xxxxx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top