I don't believe there is a way to put it in one line (at least not in
VB.NET, other languages might have a built-in method that does this or
possibly give you the ability to put multiple commands on one line), but
here is a simple loop that does what you want in VB.NET:
Dim teststring As String = "I want to go to the park, and then go home."
Dim i As Integer = -1
Dim stringcount As Integer = 0
While teststring.IndexOf("go", i + 1) <> -1
stringcount += 1
i = teststring.IndexOf("go", i + 1)
End While
You can also write it as a function, which I would strongly suggest if you
plan on doing this more than once:
Public Function CountInstances(ByVal lookfor As String, ByVal lookin As
String) As Integer
Dim stringcount As Integer = 0
Dim i As Integer = -1
While lookin.IndexOf(lookfor, i + 1) <> -1
stringcount += 1
i = lookin.IndexOf(lookfor, i + 1)
End While
Return stringcount
End Function
Writing it as a function will require the few lines of code to write the
function, but after that you can call it from just one line, making your
code simpler to write and debug:
stringcount = CountInstances("go", teststring)
If you have any questions, feel free to ask. Good Luck!