给一个计数器法组合字符串的方法,在新工程里添加一个按钮,拷贝下列代码运行即看到效果
Private Function inc(s() As String, c() As Integer, l As Integer) As String
'返回计数器指示的字符组合,同时计数器+1
Dim i As Integer, a As String, j As Integer
a = ""
If c(0) < 0 Then Exit Function
For i = 0 To l
a = a & Mid(s(i), c(i) + 1, 1)
Next
j = 1
For i = 0 To l
c(i) = c(i) + j
j = 0
If c(i) + 1 > Len(s(i)) Then
c(i) = 0
j = 1
End If
Next
If j = 1 Then c(0) = -1
inc = a
End Function
Private Sub Command1_Click()
Dim c(2) As Integer, s(2) As String, a As String, i
s(0) = "AB": s(1) = "123": s(2) = "XY"
c(0) = 0: c(1) = 0: c(2) = 0: a = "": i = 0
While c(0) >= 0
a = a & inc(s, c, 2) & ","
i = i + 1
Wend
MsgBox a & i & "种组合"
End Sub