Use
Array.IndexOf
. With this method, you don't even need
Contains
:
IndexOf
returns
-1
if the item doesn't exist:
Dim s As String() = {"first", "second", "third"}
Dim l As String = "third"
Dim i As Integer = Array.IndexOf(s, l)
If i >= 0 Then
Console.WriteLine("it exists at index " + i.ToString())
End If
Important: if you want to use
StringComparer.CurrentCultureIgnoreCase
, then you cannot use the above; you'll have to use
FindIndex
in that case:
Dim i As Integer = Array.FindIndex(s, Function(x As String) x.Equals(l, StringComparison.CurrentCultureIgnoreCase))