VB.NET 中带有字符串数组的 IndexOf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3670244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:02:28  来源:igfitidea点击:

IndexOf with String array in VB.NET

vb.netindexofarrays

提问by Eugene

How would I find the index of an item in the string array in the following code:

我将如何在以下代码中找到字符串数组中项目的索引:

Dim arrayofitems() as String
Dim itemindex as UInteger
itemindex = arrayofitems.IndexOf("item test")
Dim itemname as String = arrayofitems(itemindex)

I'd like to know how I would find the index of an item in a string array. (All of the items are lowercase, so case shouldn't matter.)

我想知道如何在字符串数组中找到项目的索引。(所有项目都是小写,所以大小写无关紧要。)

回答by Hans Olsson

It's a static (Shared) method on the Arrayclass that accepts the actual array as the first parameter, as:

它是类Shared上的一个静态 ( ) 方法,Array它接受实际数组作为第一个参数,如下所示:

Dim arrayofitems() As String
Dim itemindex As Int32 = Array.IndexOf(arrayofitems, "item test")
Dim itemname As String = arrayofitems(itemindex)

MSDN page

MSDN页面

回答by Ito

Array.FindIndex(arr, (Function(c As String) c=strTokenKey)

Array.FindIndex(arr, (Function(c As String) c.StartsWith(strTokenKey)))

回答by Oded

IndexOfwill return the index in the array of the item passed in, as appears in the third line of your example. It is a static (shared) method on the Arrayclass, with several overloads- so you need to select the correct one.

IndexOf将返回传入项的数组中的索引,如示例的第三行所示。它是类上的一个静态(共享)方法Array,具有多个重载- 因此您需要选择正确的一个。

If the array is populated and has the string "item test" as one of its items then the following line will return the index:

如果数组已填充并且将字符串“item test”作为其项目之一,则以下行将返回索引:

itemindex = Array.IndexOf(arrayofitems, "item test")

回答by epotter

For kicks, you could use LINQ.

对于踢球,您可以使用LINQ

Dim items = From s In arrayofitems _
        Where s = "two" _
        Select s Take 1

You would then access the item like this:

然后,您将像这样访问该项目:

items.First