Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String creation (specified length)

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.

like image 602
Jeremy Avatar asked Aug 21 '09 16:08

Jeremy


2 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.

like image 161
bdukes Avatar answered Sep 28 '22 12:09

bdukes


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.

like image 20
Brian Rasmussen Avatar answered Sep 28 '22 12:09

Brian Rasmussen