Count Specific Substring Occurrences in String


strText = "QTP Sreenu QTP Blog"
strSub = "qtp"'Counting the number of occurrences of qtp

Using Split Function

MsgBoxUBound(Split(UCase(strText),UCase(strSub)))

Using Len & Replace Funtions

MsgBox (Len(strText)-Len(Replace(UCase(strText),UCase(
strSub),"")))/Len(strSub)

Using Len & InStr Functions

iLenMain = Len(strText)
iLenSub = Len(strSub)
iCount=0
i=0
DoWhile i<iLenMain
    iCharPosition = InStr(i+1,UCase(strText),UCase(strSub))
    If iCharPosition>0Then
        iCount = iCount+1
        i = iCharPosition + iLenSub
    Else
        ExitDo
    EndIf
Loop
MsgBox iCount

No comments:

Post a Comment