vb.net, why doesn't this work?

Soldato
Joined
18 Oct 2002
Posts
6,889
I'm doing a "debugging" section on a guide and i can't work out why this doesn't work:

Dim i As Integer
Dim lettercount As Integer
Dim strText As String
Dim letter As Char

strText = "Debugging"

For i = 1 To strText.Length - 1
letter = strText.Substring(0)

If letter = "g" Then
lettercount = lettercount + 1
End If
Next

TextBox1.Text = "G appears " & lettercount & " Times"


I know i have to advance the letter it is checking, but i don't know how? I am a n00b if you hadn't already guessed :)

B@
 
You need to change these lines:

Code:
For i = 0 To strText.Length - 1 // Substring is zero-based
letter = strText.Substring(i) // this will take care of progressing along the string as the loop iterates.

If letter.ToLower() = "g" Then // this will make the search case insensitive - if you need it to be

It would perform better if you wrote this with a regular expression, but I can't remember what the code would be for that off the top of my head :)
thanks, that's perfect!

It's a bit cheeky of the guide tbh as there's been one small section on loops and no proper discussion of substrings etc.. and just sort'a says "fix it", with no solution, meh.

thanks again.

B@
 
Back
Top Bottom