vb.net 如何从vb.net中包含数组的函数中获取元素的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29470861/
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-17 18:57:58  来源:igfitidea点击:

How to get the index of element from contains function of array in vb.net

arraysvb.net

提问by Sahil Manchanda

i have a list of large number of string elements in array and i am using contains function to check if it contains that element. it is working fine. Now i want to get to know the index/position of element. suppose the array is

我在数组中有大量字符串元素的列表,我正在使用 contains 函数来检查它是否包含该元素。它工作正常。现在我想知道元素的索引/位置。假设数组是

dim s as string() = {"first", "second","third"}

and the string

和字符串

dim l as string = "third"

method

方法

dim b as boolean = s.Contains(l, StringComparer.CurrentCultureIgnoreCase)

flag

旗帜

if (b) Then
messagebox.show("It exists") 
end if

above array is just an example. original array consists of 7690 entries and each entry is written in utf-8 and indexOf function is not giving any result

上面的数组只是一个例子。原始数组由 7690 个条目组成,每个条目都用 utf-8 编写,indexOf 函数没有给出任何结果

采纳答案by Eminem

First of all, you should consider using a List(Of T)when writing in VB.Net.

首先,在 VB.Net 中编写时,您应该考虑使用List(Of T)

The List class provides an List(Of T).IndexOf Method (T).

List 类提供了一个List(Of T).IndexOf Method (T)

You can do something like this :

你可以这样做:

Dim index As Integer = YourList.IndexOf(l)

回答by ChicagoMike

I believe you're looking for the IndexOf function.

我相信您正在寻找IndexOf 函数

UPDATE: I came up with the following quick example that encoded a string similar to your example string into UTF-8 and it still works:

更新:我想出了以下快速示例,该示例将类似于您的示例字符串的字符串编码为 UTF-8,但它仍然有效:

    Dim s As String() = {"first", "second", "third", "four", "five", "six"}

    For Each tempString As String In s
        Dim bytes As Byte() = Encoding.Default.GetBytes(tempString)
        tempString = Encoding.UTF8.GetString(bytes)
    Next

    Dim l As String = "six"
    Debug.Print(Array.IndexOf(s, l))