【问题标题】:How to get the index of element from contains function of array in vb.net如何从vb.net中包含数组的函数中获取元素的索引
【发布时间】:2015-04-06 12:00:54
【问题描述】:

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

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

和字符串

dim l as string = "third"

方法

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

标志

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

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

【问题讨论】:

  • 如果数组确实包含你的字符串,它也应该能够给你字符串的索引。

标签: arrays vb.net


【解决方案1】:

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

List 类提供List(Of T).IndexOf Method (T)

你可以这样做:

Dim index As Integer = YourList.IndexOf(l)

【讨论】:

    【解决方案2】:

    我相信您正在寻找IndexOf function

    更新:我想出了以下快速示例,它将与您的示例字符串类似的字符串编码为 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))
    

    【讨论】:

    • 有点像。 indexOf 函数不起作用。我目前正在处理一个包含 7690 个条目的数组,每个条目/字符串都是 utf-8 格式
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-09-11
    • 2011-04-15
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多