VB.NET 在数组中查找字符串

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

VB.NET Find a string in an Array

arraysvb.net

提问by CromeX

I am new to VB.net, usually a Python or Matlab programmer.I have begun programming in VB.Net. I am battling to reference an index of a string in an array without looping through a for loop

我是 VB.net 的新手,通常是 Python 或 Matlab 程序员。我已经开始在 VB.Net 中编程。我正在努力在不遍历 for 循环的情况下引用数组中字符串的索引

How can I find an entry in an array in one line? My thinking is this..

如何在一行中找到数组中的条目?我的想法是这样..

Dim indx As Integer
Dim MyArray() As String   

indx = MyArray.find("ThisEntry")

or the index of

或索引

indx = MyArray.indexof("ThisEntry")

So far all I have found is function describing a method directly after you declare the variable? Am I missing something? or does the logic not make sense?

到目前为止,我发现的只是在声明变量后直接描述方法的函数?我错过了什么吗?或者逻辑没有意义?

回答by Carlos Landeras

Do it this way, after you have some content on your array, that now is empty:

这样做,在您的阵列上有一些内容后,现在是空的:

Dim result As String = Array.Find(MyArray, Function(s) s = "ThisEntry")

To get the index:

获取索引:

Dim index As Integer = Array.FindIndex(MyArray, Function(s) s = "ThisEntry")

回答by Douglas Barbin

Dim MyArray() As String = {"a", "ThisEntry", "b"}
Dim indx As Integer = MyArray.ToList().IndexOf("ThisEntry")

回答by the_lotus

IndexOf works, you're just not using it correctly.

IndexOf 有效,只是您没有正确使用它。

Dim arr As String() = {"aa", "bb", "cc"}

index = Array.IndexOf(arr, "bb")

回答by Yaugen Vlasau

Sub Main()
    Dim numbers As String() = {"aaa", "bbb", "ccc"}

    Console.WriteLine(numbers.ToList().FindIndex(Function(x) x = "bbb"))
End Sub