VB/VBS/JS/ASP的StringBuilder实现
提醒:本页面将不再更新、维护或者支持,文章、评论所叙述内容存在时效性,涉及技术细节或者软件使用方面不保证能够完全有效可操作,请谨慎参考!
Visual Basic版本
vbAccelerator的Steve McMahon给我们提供了一个好用的cStringBuilder类,便于我们实现StringBuilder的功能,据作者讲添加10,000次类似于"http://vbaccelerator.com/"这样字符串,标准VB方式需要34秒,而使用Steve McMahon的cStringBuilder类只需要0.35秒。效率和速度还是相当不错的。
这个类的使用方式及下载可以访问其 项目主页 。
JavaScript版本
虽然JS不像C#那样自带有StringBuilder方法,但是我们可以变通一下,从而达到StringBuilder的效果,其实我们可以使用内置数组(array),然后将字符串push进array中,然后通过数组的join办法,一次性组合成新字符串。
这里有个 来自CodeProject的示例 。
// Initializes a new instance
// of the StringBuilder class
// and appends the given value
// if supplied
function StringBuilder(value) {
this.strings = new Array("");
this.append(value);
}
// Appends the given value
// to the end of this instance.
StringBuilder.prototype
.append = function (value) {
if (value) {
this.strings.push(value);
}
}
// Clears the string buffer
StringBuilder.prototype
.clear = function () {
this.strings.length = 1;
}
// Converts this instance
// to a String.
StringBuilder.prototype
.toString = function () {
return this.strings.join("");
}
另外Ferreri Gabriele在 《Faster JavaScript StringBuilder》 提供了一个据说效率更高的版本,大家也可以看看。
ASP/VBScript版本
其实上面的JS代码是可以直接改写成ASP代码的,好吧,重点不在这里,对于深入研究过ASP的童鞋们来说, 《Improving String Handling Performance in ASP Applications》 这篇文章肯定看过。作者James Musson通过介绍几种字符串操作方式为我们展示了ASP(其实也可以说是VBScript)在字符串操作性能上的差异。很明显,旧式的字符串拼接速度已经远不能满足我们对大批量字符串处理的要求,所以我们需要StringBuilder,James Musson在文章中给出了VB的实现方式,对于ASP/VBScript的实现我们还是看下 《Classic ASP String Builder Class》 这篇文章,核心代码如下:
Class StringBuilder
'the array of strings to concatenate
Private arr
'the rate at which the array grows
Private growthRate
'the number of items in the array
Private itemCount
Private Sub Class_Initialize()
growthRate = 10
itemCount = 0
ReDim arr(growthRate)
End Sub
'Append a new string to the end of the array. If the
'number of items in the array is larger than the
'actual capacity of the array, then "grow" the array
'by ReDimming it.
Public Sub Append(ByVal strValue)
'code borrowed from FastString to prevent crash on NULL'
strValue=strValue & ""
If itemCount > UBound(arr) Then
ReDim Preserve arr(UBound(arr) + growthRate)
End If
arr(itemCount) = strValue
itemCount = itemCount + 1
End Sub
'Concatenate the strings by simply joining your array
'of strings and adding no separator between elements.
Public Function ToString()
ToString = Join(arr, "")
End Function
End Class
2011年11月23日更新
偶然一次逛StackOverflow时看到一种 利用.NET Framework创建ActiveX COM 组件实现StringBuilder 的办法,代码也不错,如果目标机器上装有.NET Framework,这个也不失为一个较好的解决办法,相关代码如下:
'********************************************************************
' Use this class to concatenate strings in a much more
' efficient manner than simply concatenating a string
' (strVariable = strVariable & "your new string")
Class StringBuilder
'PURPOSE: this class is designed to allow for more efficient string
' concatenation.
'DESIGN NOTES:
' Originally, this class built an array and used Join in the ToString
' method. However, I later discovered that the System.Text.StringBuilder
' class in the .NET Framework is COMVisible. That means we can simply use
' it and all of its efficiencies rather than having to deal with
' VBScript and its limitations.
Private oStringBuilder
Private Sub Class_Initialize()
Set oStringBuilder = CreateObject("System.Text.StringBuilder")
End Sub
Private Sub Class_Terminate( )
Set oStringBuilder = Nothing
End Sub
Public Sub InitializeCapacity(ByVal capacity)
On Error Resume Next
Dim iCapacity
iCapacity = CInt(capacity)
If Err.number <> 0 Then Exit Sub
oStringBuilder.Capacity = iCapacity
End Sub
Public Sub Clear()
Call Class_Initialize()
End Sub
Public Sub Append(ByVal strValue)
Call AppendFormat("{0}", strValue)
End Sub
Public Sub AppendFormat(ByVal strFormatString, ByVal strValue)
Call oStringBuilder.AppendFormat(strFormatString, _
(strValue & vbNullString))
End Sub
'Appends the string with a trailing CrLf
Public Sub AppendLine(ByVal strValue)
Call Append(strValue)
Call Append(vbCrLf)
End Sub
Public Property Get Length()
Length = oStringBuilder.Length
End Property
Public Property Let Length( iLength )
On Error Resume Next
oStringBuilder.Length = CInt(iLength)
End Property
'Concatenate the strings by simply joining your array
'of strings and adding no separator between elements.
Public Function ToString()
ToString = oStringBuilder.ToString()
End Function
End Class
如果需要改成ASP版本的StringBuilder类,请将CreateObject换成Server.CreateObject会好些。