This code:
Module Module1
Sub Main()
' Our input string.
Dim animals As String = "cat, dog, bird"
' See if dog is contained in the string.
If Not animals.IndexOf("dog") = -1 Then
Console.WriteLine(animals.IndexOf("dog"))
End If
End Sub
End Module
Return start position 5
in string
But how to return index of string so:
for cat = 1
for dog = 2
for bird = 3
Best How To :
Looking at your desired output it seems you want to get the index of word in your string. You can do this by splitting the string to array and then finding the item in an array using method Array.FindIndex:
Dim animals = "cat, dog, bird"
' Split string to array
Dim animalsArray = animals.Split(New String() {",", " "}, StringSplitOptions.RemoveEmptyEntries)
' Item to find
Dim itemToFind = "dog"
' Find index in array
Dim index = Array.FindIndex(Of String)(animalsArray, Function(s) s = itemToFind)
' Add 1 to the output:
Console.WriteLine(index + 1)
Above code returns 2. For cat you would get 1 and for bird the result would be 3. If there is no item in the array the output would be 0