VB.NET 中的 IndexOf 与字符串数组

发布于 2024-09-18 15:18:04 字数 256 浏览 2 评论 0 原文

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

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

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

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花期渐远 2024-09-25 15:18:05

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

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

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

IndexOf will 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 Array class, with several overloads - so you need to select the correct one.

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

itemindex = Array.IndexOf(arrayofitems, "item test")
假装不在乎 2024-09-25 15:18:05
Array.FindIndex(arr, (Function(c As String) c=strTokenKey)

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

Array.FindIndex(arr, (Function(c As String) c.StartsWith(strTokenKey)))
游魂 2024-09-25 15:18:05

如果需要刺激,您可以使用 LINQ

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

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

items.First

For kicks, you could use LINQ.

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

You would then access the item like this:

items.First
你好,陌生人 2024-09-25 15:18:04

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

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

MSDN 页面

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

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

MSDN page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文