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

后端 未结 12 2009
逝去的感伤
逝去的感伤 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:42

    I know this thread is really old, but I got another solution too:

    Function countOccurencesOf(needle As String, s As String)
        Dim count As Integer = 0
        For i As Integer = 0 to s.Length - 1
            If s.Substring(i).Startswith(needle) Then
                count = count + 1
            End If
        Next
        Return count
    End Function
    

提交回复
热议问题