how to find the number of occurrences of a substring within a string vb.net

后端 未结 12 1989
逝去的感伤
逝去的感伤 2020-12-06 02:14

I have a string (for example: \"Hello there. My name is John. I work very hard. Hello there!\") and I am trying to find the number of occurrences of the string

12条回答
  •  自闭症患者
    2020-12-06 02:29

    I used this in Vbscript, You can convert the same to VB.net as well

    Dim str, strToFind
    str = "sdfsdf:sdsdgs::"
    strToFind = ":"
    
    MsgBox GetNoOfOccurranceOf( strToFind, str)
    
    Function GetNoOfOccurranceOf(ByVal subStringToFind As String, ByVal strReference As String)
        Dim iTotalLength, newString, iTotalOccCount
        iTotalLength = Len(strReference)
        newString = Replace(strReference, subStringToFind, "")
        iTotalOccCount = iTotalLength - Len(newString)
        GetNoOfOccurranceOf = iTotalOccCount
    End Function
    

提交回复
热议问题