The following code is supposed to show in a message box the index of the third a in the string checkme, but when I run the program it doesn’t give me the right answer (should be 12, instead I get 9). What am I doing wrong and how would I go about making it work?
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim checkme As String = 'thais is a sample sentence a' Dim indexnum As Integer indexnum = checkme.IndexOf('a', checkme.IndexOf('a') + 3) MessageBox.Show(CStr(indexnum)) End Sub
Option Explicit and Strict must be On. Thanks in advance.
You’re only finding the second ‘a’. Try using a loop like so.