VB字符的操作

来源:百度知道 编辑:UC知道 时间:2024/05/16 13:31:25
比如说文本框1有连续的数字,求出"1"最多连续几次,最多间隔几次.
比如说下面这个,"1"最多连续4次,最长间隔6次.
.
0
1
0
1
0
0
1
1
1
1
0
0
1
1
0
1
0
0
0
0
0
0

Private Sub Command1_Click()
Dim a As String
a = Trim(Text1.Text)
a = Replace(a, vbCrLf, "") '用于替换相同的间隔符
con = 0
conmax = 0
nocon = 0
noconmax = 0
For i = 1 To Len(a)
If Mid(a, i, 1) = "1" Then
con = con + 1
If nocon > noconmax Then noconmax = nocon
nocon = 0
Else
nocon = nocon + 1
If con > conmax Then conmax = con
con = 0
End If
Next
If nocon > noconmax Then noconmax = nocon
If con > conmax Then conmax = con

Print "最多连续"; conmax, "最多间隔"; noconmax
End Sub

呵呵,分数挺高,我先占位。马上给代码: