StringAccumulator component

At some point, every VB programmer has written a loop something like this:

Dim i As String, inl As String


While Not EOF(inf)

Line Input #inf, inl


' {... do stuff to inl ...}


i = i + inl



Debug.Print i

You know, the old "build a string out of pieces" loop.

While we all end up performing this task, the truth is, it's a really inefficient way to build strings. Every time the "i = i + inl" code executes, VB will allocate a new string, copy the current contents of i into it, copy the contents of inl into it, assign it to i, and deallocate the old string that contained i's former contents. If you are building large strings from many fragments this kind of loop can completely devastate your application's performance.

Since I often found myself writing this kind of loop when processing HTML files, I added a component to the HtmlZap library designed to address the performance problems caused by building strings from fragments. That component is called StringAccumulator.

The StringAccumulator component is very simple. All it does is allow you to build a string by appending fragments, then retrieve the accumulated string.

However, using the StringAccumulator component is far faster than the technique shown in the above example. Instead of doing a lot of copying and memory management, the StringAccumulator component builds a linked list of the strings you append. It only builds the complete string when you retrieve the component's String property, and even then it allocates only one string (big enough to contain all the fragments) and copies all the fragments into it.

Rewriting the above code fragment using the StringAccumulator component would result in something like this:

Dim inl As String

Dim sa As New HTMLZAPLib.StringAccumulator

While Not EOF(inf)

Line Input #inf, inl


' {... do stuff to inl ...}


sa.Add inl



Debug.Print sa.String

Building strings with StringAccumulator is so efficient that I often process entire HTML files by opening the file, running it through HtmlZap to parse the tags, adding the modified tags and text to a StringAccumulator object, and then writing the contents of the StringAccumulator back out to disk. This saves the whole "rename the file to .BAK, open it, write to original name, and delete .BAK file" cycle.


StringAccumulator Properties and Methods

Properties Methods
For the purposes of the sample code in this document, the object name "SA" refers to an instance of the StringAccumulator control.
StringAccumulator properties and methods are only useful at runtime.


Method Add

  • Usage: object.Add string

Adds string to the StringAccumulator's contents.

Method AddCR

  • Usage: object.AddCR string

Adds string plus a carriage-return/linefeed pair to the StringAccumulator's contents.

Property IsEmpty

  • Boolean
  • Read-only

This boolean value is True when the StringAccumulator is empty.

Property Length

  • Integer
  • Read-only

Retrieves the current length of the StringAccumulator's contents.

Method Reset

  • Usage: object.Reset

Empties the StringAccumulator.

Property String

  • String
  • Read-write. Usage: string = object.String

Retrieves or sets the StringAccumulator's current contents. Typically this property is used to retrieve the string built by calling the Add and AddCR methods, though it can also be used to initialize the object to a specific string.

Last revised: 30 December 2001