Below is the sample code for finding how many time which one word or string occur in paragraph or content. I use one Textbox to store whole text and another one Textbox for input word that we want to find by using ButtonClike_Event.
Application Design
No | Control Type | Name | Text |
1 | TextBox1 | TextBoxWholeString | |
2 | TextBox2 | TextBoxFind | |
3 | Button1 | ButtonFind | Find |
4 | Label1 | LabelShow |
Code
Public Class StringOccur
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
Private Sub ButtonFind_Click(sender As Object, e As EventArgs) Handles ButtonFind.Click
Try
LabelShow.Text = "The word " & TextBoxFind.Text & " has occured " & FindWords(TextBoxWholeString.Text, TextBoxFind.Text) & " times!!"
Catch ex As Exception
End Try
End Sub
End Class
0 comments:
Post a Comment