VB textbox中字符判断出现次数并按多至少排列
VB textbox中字符判断出现次数并按多至少排列
新建一个vb窗体
加入两个textbox 分别是text1和text2
加入一个Command1
如果在text1中输入如下数据
王小虎 李小冰 林小明 王小虎 李小冰 林小明
王小虎 王小虎 杨东
点击Command1时
text2对text1输入的字符进行判断出现次数并按多至少排列
text2内容如下显示
出现4次:王小虎 '换行
出现2次:李小冰 林小明 '换行
出现1次:杨东
应该如何写代码呢.?
[解决办法]
Private Sub Command1_Click()
Dim StrSS$, s$(), i&, j&, n&, Pass%
ReDim ss$(1, 0), s1$(0)
StrSS = Replace(Form1.Text1.Text, vbCrLf, Space(1))
s = Split(StrSS, Space(1))
For i = 0 To UBound(s)
If s(i) = "王小虎" Or s(i) = "李小冰" Or s(i) = "林小明" Or s(i) = "杨东" Then '固定查此4人(加这里更合理)
Pass = 0
If Trim(s(i)) <> "" Then
For j = 0 To UBound(ss, 2)
If ss(0, j) = Trim(s(i)) Then Pass = 1: ss(1, j) = Val(ss(1, j)) + 1: Exit For
Next j
If Pass = 0 Then
ReDim Preserve ss(1, n)
ss(0, n) = Trim(s(i)): ss(1, n) = 1: n = n + 1
End If
End If
End If
Next i
n = 0
For i = 0 To UBound(ss, 2)
Pass = 0
For j = 0 To UBound(s1)
If 0 < InStr(s1(j), "出现" & ss(1, i) & "次:") Then Pass = 1: s1(j) = s1(j) & "," & ss(0, i): Exit For
Next j
If Pass = 0 Then
ReDim Preserve s1(n)
s1(n) = "出现" & ss(1, i) & "次:" & ss(0, i): n = n + 1
End If
Next i
MsgBox Join(s1, vbCrLf)
End Sub