出自ProgWiki
程式碼
<%
Class StringBuilder
Private m_nLimit
Private m_strings(1000)
Private m_nPos
Public m_sText
Private Sub Class_Initialize
m_nLimit = 1000
m_strings(0) = ""
Call Clear
End Sub
Private Sub Class_Terminate
End Sub
Public Sub Clear()
m_nPos = 0
m_sText = ""
End Sub
Private Sub Shrink()
m_sText = ""
For nArg = 1 To m_nPos
m_sText = m_sText & m_strings(nArg)
Next
End Sub
Public Function Append(ByVal arrParam)
If (m_nPos = m_nLimit) Then
Call Shrink
m_strings(1) = m_sText
m_nPos = 1
m_sText = ""
End If
If (IsArray(arrParam)) Then
For nArg = 0 To UBound(arrParam)
m_nPos = m_nPos + 1
m_strings(m_nPos) = CStr(arrParam(nArg))
Next
Else
m_nPos = m_nPos + 1
m_strings(m_nPos) = CStr(arrParam)
End If
End Function
Public Function AppendFormat(ByVal sFormat, ByVal arrParam)
If (m_nPos = m_nLimit) Then
Call Shrink
m_strings(1) = m_sText
m_nPos = 1
m_sText = ""
End If
If (IsArray(arrParam)) Then
sTemp = CStr(sFormat)
For nArg = 0 To UBound(arrParam)
sTemp = Replace(sTemp, "{"&CStr(nArg)&"}", CStr(arrParam(nArg)))
Next
m_nPos = m_nPos + 1
m_strings(m_nPos) = sTemp
Else
m_nPos = m_nPos + 1
m_strings(m_nPos) = Replace(CStr(sFormat), "{0}", CStr(arrParam))
End If
End Function
Public Function ToString()
If m_nPos > 0 Then
Call Shrink
ToString = m_sText
Else
ToString = ""
End If
End Function
End Class
%>
測試
<!--#include file="StringBuilder.asp" -->
<%
Set x = new StringBuilder
with x
.Append(Array("1.","It is Test","One."))
'VB 語法的基本觀念:若是左邊沒有變數及等號承接,且輸入2個以上的傳入參數的話,Function 要當 Sub 用
.AppendFormat "I {0} {1}", Array("am","Test")
.Append("2")
.Append("3")
.Append("4")
end with
%>
<%=x.ToString() %>
<%
Set x = Nothing
%>
相關