VB/VBS/JS/ASP的StringBuilder实现

!本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使用方面,本人不保证相应的兼容和可操作性。

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的示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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》这篇文章,核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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,这个也不失为一个较好的解决办法,相关代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'********************************************************************
' 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会好些。

若无特别说明,本网站文章均为原创,原则上这些文章不允许转载,但是如果阁下是出于研究学习目的可以转载到阁下的个人博客或者主页,转载遵循创作共同性“署名-非商业性使用-相同方式共享”原则,请转载时注明作者出处谢绝商业性、非署名、采集站、垃圾站或者纯粹为了流量的转载。谢谢合作!
请稍后...

发表评论

电子邮件地址不会被公开。 必填项已用*标注