views:

239

answers:

3

Is there a succinct way (i.e. not a for loop) to create a string of a specified length? Doesn't matter what is in the string.

+20  A: 

You can use the string constructor that takes a char and an int. It creates a string instance with the char repeated the specified number of times.

bdukes
+12  A: 

As bdukes mentions there's constructor, that takes a char and an int. That will construct a string of the given length filled with the char.

However, keep in mind, that strings are immutable in .NET, so if you want to create a specific string buffer, you should use StringBuilder instead.

Brian Rasmussen
A: 

why would you want to create a string if you dont want to control what the value of the string would be?

I would suggest using a StringBuilder and using the constructor that takes an int

Petoj
For unit testing edge cases
Jeremy