-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathcStrBuilder.cls
68 lines (60 loc) · 1.82 KB
/
cStrBuilder.cls
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
65
66
67
68
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cStrBuilder"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
'用法
'dim ss as cStrBuilder
'set ss = new cStrBuilder
's.Append "test"
'msgbox s.toString()
' The secret to this class is that it uses the join
'function which is part of the VBA.Strings Class
Private mvarStringArray() As String
Private mvarArrayItems As Long
Public Sub Append(ByVal newStr As String)
ReDim Preserve mvarStringArray(mvarArrayItems) As String
mvarStringArray(mvarArrayItems) = newStr
mvarArrayItems = mvarArrayItems + 1
End Sub
Public Property Get toString(Optional sDelimiter As String = "") As String
If mvarArrayItems > 0 Then
toString = Join(mvarStringArray, sDelimiter)
Else
toString = ""
End If
End Property
Public Sub Reset()
mvarArrayItems = 0
Erase mvarStringArray
End Sub
Private Sub Class_Initialize()
If mvarArrayItems > 0 Then Reset
End Sub
Private Sub Class_Terminate()
Reset
End Sub
'判断一个字符串是否已经存在,这个是判断每次append进来的整个字符串,而不是判断子字符串
Public Function ExistString(s As String) As Boolean
Dim i As Long, nLen As Long
nLen = Len(s)
For i = 0 To mvarArrayItems - 1
If nLen = Len(mvarStringArray(i)) Then
If s = mvarStringArray(i) Then
ExistString = True
Exit Function
End If
End If
Next
End Function