String Concatenation in ASP Classic

ASP (VBScript) does string concatenation (joining) with the ampersand (&). It’s a bad performer. I’ve heard that for years. I never really understood how or why.

Marcus Tucker exposes why in the best description of the issue I’ve read.

And since I’ll probably use this again somewhere, here’s his String Concatenation class.

'-------------------------------------------
'String concatenation class (using an array)
'-------------------------------------------
'Written by Marcus Tucker, July 2004
'http://marcustucker.com
'-------------------------------------------
Class StrConCatArray
	Private StringCounter
	Private StringArray()
	Private StringLength
	
	'called at creation of instance
	Private Sub Class_Initialize()
		StringCounter = 0
		InitStringLength = 128
		ReDim StringArray(InitStringLength - 1)
		StringLength = InitStringLength
	End Sub
	
	Private Sub Class_Terminate()
		Erase StringArray
	End Sub

	'add new string to array
	Public Sub Add(byref NewString)
		StringArray(StringCounter) = NewString
		StringCounter = StringCounter + 1
		
		'ReDim array if necessary
		If StringCounter MOD StringLength = 0 Then
			'redimension
			ReDim Preserve StringArray(StringCounter + StringLength - 1)
			
			'double the size of the array next time
			StringLength = StringLength * 2
		End If
	End Sub
	
	'return the concatenated string
	Public Property Get Value
		Value = Join(StringArray, "")
	End Property 
	
	'resets array
	Public Function Clear()
		StringCounter = 0
		
		Redim StringArray(InitStringLength - 1)
		StringLength = InitStringLength
	End Function		
End Class 

Leave a comment

Your email address will not be published. Required fields are marked *