vb.net array indexof example

在 VB.NET 中,您可以使用 Array.IndexOf 方法来查找数组中特定元素的索引位置。该方法返回一个整数,表示找到的元素的索引位置。如果数组中没有找到元素,则返回-1。

下面是一个示例代码,演示如何使用 Array.IndexOf 方法查找数组中特定元素的索引位置:

' 创建一个包含一些字符串的数组
Dim myArray() As String = {"apple", "banana", "cherry", "date"}
' 查找“cherry”字符串在数组中的索引位置
Dim index As Integer = Array.IndexOf(myArray, "cherry")
' 如果找到了,输出其索引位置
If index > -1 Then
    Console.WriteLine("The index of 'cherry' is: " & index)
    Console.WriteLine("'cherry' was not found in the array.")
End If

上述代码首先创建了一个包含四个字符串的数组。然后,它使用 Array.IndexOf 方法查找“cherry”字符串在数组中的索引位置,并将结果存储在变量 index 中。最后,如果找到了“cherry”,则输出其索引位置;否则输出“cherry”没有在数组中找到。

注意,该方法只能用于一维数组,如果要查找多维数组中的元素,则需要使用其他方法或自己实现查找算法。

  •