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

后端 未结 12 1942
逝去的感伤
逝去的感伤 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
    
    0 讨论(0)
  • One more solution based on InStr(i, str, substr) function (searching substr in str starting from i position, more info about InStr()):

    Function findOccurancesCount(baseString, subString)
        occurancesCount = 0
        i = 1
        Do
            foundPosition = InStr(i, baseString, subString) 'searching from i position
            If foundPosition > 0 Then                       'substring is found at foundPosition index
                occurancesCount = occurancesCount + 1       'count this occurance
                i = foundPosition + 1                       'searching from i+1 on the next cycle
            End If
        Loop While foundPosition <> 0
        findOccurancesCount = occurancesCount
    End Function
    

    As soon as there is no substring found (InStr returns 0, instead of found substring position in base string), searching is over and occurances count is returned.

    0 讨论(0)
  • 2020-12-06 02:36

    the best way to do it is this:

    Public Function countString(ByVal inputString As String, ByVal stringToBeSearchedInsideTheInputString as String) As Integer
        Return System.Text.RegularExpressions.Regex.Split(inputString, stringToBeSearchedInsideTheInputString).Length -1
    
    End Function
    
    0 讨论(0)
  • 2020-12-06 02:40

    Looking at your original attempt, I have found that this should do the trick as "Split" creates an array. Occurrences = input.split(phrase).ubound

    This is CaSe sensitive, so in your case the phrase should equal "Hello there", as there is no "hello there" in the input

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-06 02:44

    Expanding on Sumit Kumar's simple solution (please upvote his answer rather than this one), here it is as a one-line working function:

    Public Function fnStrCnt(ByVal str As String, ByVal substr As String) As Integer
        fnStrCnt = UBound(Split(LCase(str), substr))
    End Function
    

    Demo:

    Sub testit()
        Dim thePhrase
        thePhrase = "Once upon a midnight dreary while a man was in a house in the usa."
        If fnStrCnt(thePhrase, " a ") > 1 Then
            MsgBox "Found " & fnStrCnt(thePhrase, " a ") & " occurrences."
        End If
    End Sub 'testit()
    
    0 讨论(0)
提交回复
热议问题