VB6 StringBuilder Class

Volte
03-22-2004, 09:36 PM
Many of you may have seen the System.Text.StringBuilder built into the .NET framework. The StringBuilder class allows you to build a string quickly and efficiently - much more speedy than the built in string operator. This class "cStringBuilder" is my attempt to reproduce the functionality of the .NET StringBuilder in VB. In my tests, it is much faster than VB6's functions; some operations which took many seconds in VB take only a fraction of a second with cStringBuilder.

How Does It Work?
Every time you append to a string using standard VB methods you have to do something like this:str = str & "Hello!"This is highly inefficient, because rather than simply adding on the word "Hello!" to the end of the 'str' variable, it has to create a new string (which in VB6 is the equivalent of a BSTR in C++) in the memory and copy the new string data into it. This leads to slowdowns and hangups when working with large amounts of string data. It is possible to speed up the process by using the Mid$ function to assign to the string, but this is less than flexible.

The cStringBuilder class takes a different approach. When you initialize the class, it pre-allocates a 1MB buffer of memory to store the string in, so the string can grow and shrink dynamically within that 1MB space and never need to reallocate any memory. The only time it needs to reallocate is when the string grows bigger than the currently allocated buffer. When the string buffer grows, it is always in increments of 1MB.

How Do I Use It?
Here is a run-down of all the functions and properties available in cStringBuilder:

Property StringData As String
Gets or sets the data stored in the StringBuilder.

Property Length As Long
Gets the length of the data stored in the StringBuilder.

Sub Clear
Resets the stored string to nothing, and resizes the in-memory buffer back to 1MB.

Sub Append(str As String)
Adds the string str on to the end of the data stored in the cStringBuilder.

Sub Insert(index As Long, str As String)
Inserts a string (str) into the StringBuilder at a specific index (index). Note that the index is zero-based, so to insert to the beginning of the string, specify 0 as the index.

Sub Overwrite(index As Long, str As String)
Inserts a string (str) into the StringBuilder at a specific index (index), overwriting the data at that point, rather than moving it to the right as is the case with Insert. If the data you insert goes past the end of the stored data, the excess will be appended normally.

Sub Remove(index As Long, length As Long)
Removes a section (length characters) of the stored string, starting at index. If length = 0, the entire string starting at index is removed.

All indexes are zero-based. Character index 0 is the first character in the string.

'Extremely short example:
Dim sb As New cStringBuilder

sb.StringData = "Hello, "
sb.Append "Wxld!"
sb.Insert 8, "o"
sb.Overwrite 9, "r"

MsgBox sb.StringData


Please report all comments, bugs, and suggestions to me via private message.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum