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.
Asked
Active
Viewed 8,583 times
3 Answers
34
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
- 152,002
- 23
- 148
- 175
12
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
- 114,645
- 34
- 221
- 317
-3
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

Peter
- 37,042
- 39
- 142
- 198