How to count the number of non-overlapping instances of a substring in a string in VB.NET

3 Answers

0 votes
Imports System

Public Class CountNumberOfNonOverlappingInstancesOfSubstringInAString_VB_NET
    Public Shared Function countOccurrences(ByVal str As String, ByVal substr As String) As Integer
        If substr.Length = 0 Then
            Return 0
        End If

        Dim count As Integer = 0
        Dim offset As Integer = str.IndexOf(substr, StringComparison.Ordinal)

        While offset <> -1
            count += 1
            offset = str.IndexOf(substr, offset + substr.Length, StringComparison.Ordinal)
        End While

        Return count
    End Function

    Public Shared Sub Main(ByVal args As String())
		Dim s As String = "go java phphp rust c pphpp c++ phpphp python php phphp"
		
        Console.WriteLine(countOccurrences(s, "php"))
    End Sub
End Class


' run:
'
' 6
'

 



answered Aug 24, 2024 by avibootz
edited 4 hours ago by avibootz
0 votes
Imports System
 
Public Class CountNumberOfNonOverlappingInstancesOfSubstringInAString_VB_NET
    Public Shared Function countOccurrences(ByVal str As String, ByVal substr As String) As Integer
        Return (str.Length - str.Replace(substr, String.Empty).Length) / substr.Length
    End Function
 
    Public Shared Sub Main(ByVal args As String())
        Dim s As String = "go java phphp rust c pphpp c++ phpphp python php phphp"
         
        Console.WriteLine(countOccurrences(s, "php"))
    End Sub
End Class
 
 
' run:
'
' 6
'

 



answered Aug 24, 2024 by avibootz
edited 4 hours ago by avibootz
0 votes
Imports System

Module MainModule

    ' Non‑overlapping occurrences are matches of a substring that do not reuse any of
    ' the same characters. Once one match is counted, the next search must begin
    ' after that match ends.

    Function count_non_overlapping(haystack As String, needle As String) As Integer
        '
        ' Count how many times 'needle' appears in 'haystack' without overlapping.
        ' The algorithm:
        '   • Use String.IndexOf() to locate the next occurrence.
        '   • Each time a match is found, move the search index forward
        '     by the full length of the matched substring.
        '   • This ensures no characters are reused between matches.
        '

        Dim count As Integer = 0
        Dim index As Integer = 0   ' current search position in the main string

        ' Continue searching until IndexOf() returns -1 (meaning: no more matches)
        While True
            ' Find the next occurrence starting at the current index
            Dim pos As Integer = haystack.IndexOf(needle, index)

            If pos = -1 Then
                ' No more matches found
                Exit While
            End If

            ' We found a match, so increment the count
            count += 1

            ' Move index forward by the length of the needle
            ' This ensures the next search begins *after* the matched substring
            index = pos + needle.Length
        End While

        Return count
    End Function


    ' ---------------------------------------------------------------
    Sub Main()
        ' Demonstration using the string provided in the instructions:
        Dim s As String = "go java phphp rust c pphpp c++ phpphp python php phphp"
        Dim substring As String = "php"

        ' Count non-overlapping occurrences
        Dim result As Integer = count_non_overlapping(s, substring)

        Console.WriteLine("Non-overlapping occurrences: " & result)
    End Sub

End Module


'
' run:
'
' Non-overlapping occurrences: 6
'

 



answered 4 hours ago by avibootz

Related questions

...