VB6:「サンプル:指定文字列内に検索文字数がいくつあるか」
え?
こんな関数が用意されていないのか・・?
はて?
まぁいいや、とりあえずオブジェクトを生成して作るとかメンドクサイというか、それは情けないのでさくっと書いてみた。
驚きだったので載せておきます。
VB6 というより、VB系のネタ全般で存在しない関数なのかな?
ま、細かい想定してないので今やりたいことはできる模様。
Sub testGetInstrCount()
Dim intCnt As Integer
intCnt = getInstrCount("AAA--BB-CC--", "AA")
msgBox CStr(intCnt)
End Sub
' 指定文字列中の検索文字が何個含まれるかを返す
' strSrc:検索対象文字列
' strSrch:検索文字
' 戻り値:検索文字列内の個数
Public Function getInstrCount(strSrc As String, strSrch As String) As Integer
Dim intCnt As Integer
Dim intDataCnt As Integer
Dim intCntBuf As Integer
For intCnt = 1 To Len(strSrc) + 1
intCntBuf = InStr(intCnt, strSrc, strSrch, vbBinaryCompare)
If intCntBuf >= 1 Then
intDataCnt = intDataCnt + 1
intCnt = intCntBuf
End If
Next
getInstrCount = intDataCnt + (Len(strSrch) - 1)End Function