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("");
} |
// 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》这篇文章,核心代码如下:
继续阅读“VB/VBS/JS/ASP的StringBuilder实现”