Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a StringBuilder initialized with a string contain exactly (only) enough space for that string?

I'm wondering if this code ...

StringBuilder sb = new StringBuilder("Please read the following messages.");

... initializes sb with a buffer exactly as large as the string passed to the constructor. On the one hand, this would seem the most logical thing. On the other hand, it seems to kind of defeat the purpose of the StringBuilder class for one of its most common uses, which is to provide mutability to make repeated appends more efficient. (The very first call to Append, if the answer to my question is "yes", would require sb to resize itself.)

Then again, I suppose one could view this as analogous to the constructor for List<T> that takes an IEnumerable<T> as a parameter. Maybe the assumption in this case is that you're not planning on appending a lot, but rather on manipulating what's already there.

The only real research I've done on this was to check the MSDN documentation on StringBuilder, which didn't provide an answer (it says the constructor initializes the instance "using the specified string," but doesn't indicate how the string is used).


EDIT: So it's "implementation-specific"... does this not seem weird to anyone else? I mean, the purpose of the StringBuilder class is to offer an alternative to performing a lot of operations on a string, creating a ton of immutable string instances along the way; therefore, it's for efficiency. I feel like the behavior of this constructor ought the be specified, so that the developer can make an informed decision how to use it regardless of platform.

I mean, it is implemented by Microsoft a certain way; they could easily have put that in the documentation (forcing other implementations to follow suit). Just a personal source of puzzlement...

like image 955
Dan Tao Avatar asked Feb 15 '10 13:02

Dan Tao


People also ask

Does string builder have a limit?

The maximum capacity for this implementation is Int32. MaxValue. However, this value is implementation-specific and might be different in other or later implementations. You can explicitly set the maximum capacity of a StringBuilder object by calling the StringBuilder(Int32, Int32) constructor.

What is the initial capacity of the string builder?

Constructs a string builder that contains the same characters as the specified CharSequence . The initial capacity of the string builder is 16 plus the length of the CharSequence argument.

What is the space complexity of StringBuilder?

Each time you append the StringBuilder it checks if the builder array is filled, if required copies the contents of original array to new array of increased size. So the space requirement increases linearly with the length of String. Hence the space complexity is O(n) .

Which of the following statements are true about string StringBuffer and StringBuilder?

Correct Answer: A, B StringBuffer and StringBuilder are mutable classes.


2 Answers

It's an implementation detail that you shouldn't need to worry about. However, using .NET reflector, and looking in the (string,int32,int32,int32) overload of the constructor (which the other constructors call), we can see that it picks a capacity that is a multiple of 16 (next largest over the requested size)

Edit

Actually, it's 16 x 2^n, with the value of "n" selected to be the next largest size

like image 104
Damien_The_Unbeliever Avatar answered Sep 21 '22 01:09

Damien_The_Unbeliever


Check the StringBuilder's Capacity member.

From MSDN:

The StringBuilder dynamically allocates more space when required and increases Capacity accordingly. For performance reasons, a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation-specific.

like image 22
devio Avatar answered Sep 21 '22 01:09

devio