我在vb6编程,我需要计算字符串中特定字符出现的次数。我知道如何在vb.net中执行此操作,但vb6的方法不同。这就是我试过的,
Dim sletter As String
Dim icount As Integer
sletter = InputBox("Enter a letter to count.")
m_sStringtosearch = TxtString.Text
For Each element In m_sStringtosearch
If element = sletter Then
icount += 1
End If
Next
MessageBox.Show(sletter & " was found " & icount & " times.")
我使用了一个变体,其中没有输入框(我已经记住了一个角色),最后没有messagebox
(我将使用icount
结果一个if语句)。
答案 0 :(得分:1)
我的VB6生锈了。这是未经测试的。但是,这应该是接近的。 VB 6没有For Each或+ =(我相信)。我也将icount初始化为0,出于偏执,虽然我相信这是VB6为你做的事情。
Dim sletter As String
Dim icount As Integer
Dim i as Integer
Dim element as String
sletter = InputBox("Enter a letter to count.")
m_sStringtosearch = TxtString.Text
icount = 0
For i = 1 to Len(m_sStringtosearch)
element = Mid$(m_sStringtosearch, 1, 1)
If element = sletter Then
icount = icount + 1
End If
Next
MessageBox.Show(sletter & " was found " & icount & " times.")
答案 1 :(得分:1)
尝试这种替代解决方案
icount = (Len(m_sStringtosearch) - Len(Replace(m_sStringtosearch, sletter, vbNullstring))) \ Len(sletter)
答案 2 :(得分:0)
Dim i As Long
Dim iCount As Long
For i = 1 To Len(m_sStringtosearch)
If LCase(Mid(m_sStringtosearch, i, 1)) = TxtString.Text Then iCount = iCount + 1
Next