washcloth
washcloth

Reputation: 2896

Retrieving location of found text in array

I have a large file that is loaded into an array and by using the .Contains method I check to see if the array contains a certain sub string.

After called the .Contains method is their a way for me to get the index of the found object(s).

Example:

Dim foo() As String = {"1","2","3"}
If foo.Contains("2") Then
   'return the index of the found number in this case 1
Else
   Return False
End If

Upvotes: 0

Views: 55

Answers (3)

George Johnston
George Johnston

Reputation: 32258

Have you tried, Array.IndexOf

e.g

var i = Array.IndexOf(yourArray, "2");

Upvotes: 3

Gray
Gray

Reputation: 7130

You want to look at Array.IndexOf

Dim foo() As String = {"1","2","3"}
Dim myIndex As Integer = Array.IndexOf(foo, "2")

It should return -1 if it is not in the array.

Upvotes: 1

Mostafa Soghandi
Mostafa Soghandi

Reputation: 1584

IndexOf will give you location of object in array. Use This:

  foo.IndexOf("2")

Upvotes: 2

Related Questions