本文整理汇总了VB.NET中System.Array.IndexOf方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Array.IndexOf方法的具体用法?VB.NET Array.IndexOf怎么用?VB.NET Array.IndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.IndexOf方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
输出:
The array contains the following values: [ 0]: the [ 1]: quick [ 2]: brown [ 3]: fox [ 4]: jumps [ 5]: over [ 6]: the [ 7]: lazy [ 8]: dog [ 9]: in [10]: the [11]: barn The first occurrence of "the" is at index 0. The first occurrence of "the" between index 4 and the end is at index 6. The first occurrence of "the" between index 7 and index 11 is at index 10.
示例2: Example
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
输出:
Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0 Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5 Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
示例3: MainClass
' 导入命名空间
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim values(10) As Integer
For i As Integer = 0 To 10
values(i) = i
Next i
Console.WriteLine(Array.IndexOf(values, 6).ToString)
Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
End Sub
End Class