Function to count how many times a string occurs.- VB.NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

The following function checks how many times a certain word occur:

Private Function FindWords(ByVal TextSearched As String, _
ByVal Paragraph As String) As Integer

        Dim location As Integer = 0

        Dim occurances As Integer = 0

        Do

            location = TextSearched.IndexOf(Paragraph, location)

            If location <> -1 Then

                occurances += 1

                location += Paragraph.Length

  

            End If

        Loop Until location = -1

        Return occurances

End Function

Then add the following code to "Check" button click event:

Private Sub Button1_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button1.Click

        MsgBox("The word " & TextBox2.Text & " has occured " & _
                        FindWords(TextBox1.Text, TextBox2.Text) & " times!!")

End Sub

Function to count how many times a string occurs.- VB.NET


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.