Tuesday, October 17, 2006

Efficient String Content Manipulation


Abstract

String objects are immutable which means that manipulations of such objects results in automatic string creations (huge memory overheads).

To overcome this overhead use the System.Text.StringBuilder class for string manipulations then when your done convert the result to a String object using the StringBuilder.ToString().

Detail

Immutable String objects mean that once created their content cannot be changed.

eg. concatenating String objects results in new String objects created whenever new elements are added to the existing string.

The overhead can exponentially increase as the number of string manipulations goes up.

The StringBuilder class offers a solution by providing a "buffer of characters" allowing you to do complex manipulations without the need for new objects being created.

Capacity tells us the size of the buffer

Length tells us the length of the current content.

If the length of the data where manipulating exceeds the capacity then a new buffer needs to be allocated. This can negate the benefits of the StringBuilder class, so take care in defining the capacity. [Default capacity is 16]

MaxCapacity tells us the maximum size of allocation. There may be issues if the size is greater than 2GB. Ensure that the Capacity does not exceed the MaxCapacity.


TIPS

Capacity is set to less than Length, System.ArgumentOutOfRangeException is thrown.

Capacity is set to something greater than MaxCapacity, System.ArgumentOutOfRangeException is thrown

Length is set to something less than the current content then the content is truncated.

Length is set to something greater than the current content then buffer is padded with spaces to meet the length.

Length is set to something greater than the Capacity then Capacity is automatically reset to the new value. Capacity will equal Length.

No comments: