首页 /编程语言和算法/VB6/VBA/ASP
 VB6 找到字符串1中出现字符串2的次数
昨天 18:47

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Sub Command1_Click()
    Debug.Print f_CountTimes("a1b12c1d12e", "12")
End Sub

Private Function f_CountTimes(strW$, strFindWord$) As Long
    ' 统计abc出现次数
    Dim lngCount&, lngPos&
    
    lngPos = InStr(strW, strFindWord)
    Do While lngPos > 0
        lngCount = lngCount + 1
        lngPos = InStr(lngPos + 1, strW, strFindWord)
    Loop
    
    f_CountTimes = lngCount
End Function

运行结果:

2


 
全部回复(0)
首页 | 电脑版 |