Fast VBScript string class

本文介绍了一种使用VBScript进行高效字符串拼接的方法。通过创建一个名为strCat的类,可以显著提高字符串构建的速度,尤其是在大量字符串拼接时。文章对比了传统方法与使用该类的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fast VBScript string class
by Jan Verhoeven, 10 November 2001


String building in VBScript is very slow. The VBScript class presented below is easy to use and much faster. 

<%
  
'fast string class
  class strCat
    
dim index, ub, ar()

    
Private Sub Class_Initialize()
      
'statements
      redim ar(50)
      index
=0
      ub
=10
    
End Sub

    
Private Sub Class_Terminate()
      
'statements
      erase ar
    
End Sub

    
public default Sub Add(value)
      ar(index)
=value
      index
=index+1
      
if index>ub then
        ub
=ub+50
        
redim preserve ar(ub)
      
end if
    
end sub

    
public Function dump
      
redim preserve ar(index-1)
      dump
=join(ar,"")
    
end function
  
end class

  
'first traditional method
  t1=time
  os
=""
  
for i=1 to 5000
    os
=os & "This is the slow method of building strings"
  
next
  t2
=time

  t3
=time
  
set cat= new strCat
  
for i=1 to 5000
    cat(
"This is the slow method of building strings")
  
next
  s
= cat.dump
  
set cat = nothing
  t4
=time

  response.Write(
"Using the & operator:" & cstr(t1) & " - " & cstr(t2) &  "<br>Using the fast string class: " & cstr(t3) &  " -  cstr(t4) &  "<br>")
%>

Using strCat
First create a new instance of the class with:

  set cat= new strCat

Next you add add substrings:

  cat("add some substring to the fast string class")
  cat("add another substring to the fast string class")

When you are done dump the result string and release the object:

  s=cat.dump
  set cat=nothing

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值