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.
-
3It always matters what's in a string, because you can't change it later.Henk Holterman– Henk Holterman2009-08-21 17:02:24 +00:00Commented Aug 21, 2009 at 17:02
-
1I am unit testing string length validation - the contents don't apply in this case.Jeremy– Jeremy2009-08-21 17:04:26 +00:00Commented Aug 21, 2009 at 17:04
Add a comment
|
3 Answers
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.
Comments
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.