如何在 VB.NET 中搜索数组?

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

How can I search an array in VB.NET?

vb.netarrays

提问by somu

I want to be able to effectively search an array for the contents of a string.
Example:

我希望能够有效地在数组中搜索字符串的内容。
例子:

dim arr() as string={"ravi","Kumar","Ravi","Ramesh"}

I pass the value is "ra" and I want it to return the index of 2 and 3.

我传递的值为“ra”,我希望它返回 2 和 3 的索引。

How can I do this in VB.NET?

我怎样才能在 VB.NET 中做到这一点?

回答by Guffa

It's not exactly clear how you want to search the array. Here are some alternatives:

目前还不清楚您想如何搜索数组。以下是一些替代方案:

Find all items containing the exact string "Ra" (returns items 2 and 3):

查找包含确切字符串“Ra”的所有项目(返回项目 2 和 3):

Dim result As String() = Array.FindAll(arr, Function(s) s.Contains("Ra"))

Find all items starting with the exact string "Ra" (returns items 2 and 3):

查找以确切字符串“Ra”开头的所有项目(返回项目 2 和 3):

Dim result As String() = Array.FindAll(arr, Function(s) s.StartsWith("Ra"))

Find all items containing any case version of "ra" (returns items 0, 2 and 3):

查找包含任何大小写版本“ra”的所有项目(返回项目 0、2 和 3):

Dim result As String() = Array.FindAll(arr, Function(s) s.ToLower().Contains("ra"))

Find all items starting with any case version of "ra" (retuns items 0, 2 and 3):

查找以任何大小写版本的“ra”开头的所有项目(返回项目 0、2 和 3):

Dim result As String() = Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra"))

-

——

If you are not using VB 9+ then you don't have anonymous functions, so you have to create a named function.

如果您使用的不是 VB 9+,那么您就没有匿名函数,因此您必须创建一个命名函数。

Example:

例子:

Function ContainsRa(s As String) As Boolean
   Return s.Contains("Ra")
End Function

Usage:

用法:

Dim result As String() = Array.FindAll(arr, ContainsRa)

Having a function that only can compare to a specific string isn't always very useful, so to be able to specify a string to compare to you would have to put it in a class to have somewhere to store the string:

拥有一个只能与特定字符串进行比较的函数并不总是很有用,因此为了能够指定要进行比较的字符串,您必须将它放在一个类中以便在某个地方存储字符串:

Public Class ArrayComparer

   Private _compareTo As String

   Public Sub New(compareTo As String)
      _compareTo = compareTo
   End Sub

   Function Contains(s As String) As Boolean
      Return s.Contains(_compareTo)
   End Function

   Function StartsWith(s As String) As Boolean
      Return s.StartsWith(_compareTo)
   End Function

End Class

Usage:

用法:

Dim result As String() = Array.FindAll(arr, New ArrayComparer("Ra").Contains)

回答by Mehrdad Afshari

Dim inputString As String = "ra"
Enumerable.Range(0, arr.Length).Where(Function(x) arr(x).ToLower().Contains(inputString.ToLower()))

回答by Shimmy Weitzhandler

In case you were looking for an older version of .NET then use:

如果您正在寻找旧版本的 .NET,请使用:

Module Module1

    Sub Main()
        Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"}
        Dim result As New List(Of Integer)
        For i As Integer = 0 To arr.Length
            If arr(i).Contains("ra") Then result.Add(i)
        Next
    End Sub

End Module

回答by Konrad Rudolph

If you want an efficient search that is often repeated, first sort the array (Array.Sort) and then use Array.BinarySearch.

如果您想要经常重复的高效搜索,请先对数组 ( Array.Sort) 进行排序,然后使用Array.BinarySearch.

回答by samirprogrammer

check this..

检查这个..

        string[] strArray = { "ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI" };
        Array.IndexOf(strArray, "C"); // not found, returns -1
        Array.IndexOf(strArray, "CDE"); // found, returns index

回答by andy geoffleonard mcqueen

compare properties in the array if one matches the input then set something to the value of the loops current position, which is also the index of the current looked up item.

如果匹配输入,则比较数组中的属性,然后将某些内容设置为循环当前位置的值,这也是当前查找项的索引。

simple eg.

简单例如。

dim x,y,z as integer
dim aNames, aIndexes as array
dim sFind as string
for x = 1 to length(aNames)

    if aNames(x) = sFind then y = x

y is then the index of the item in the array, then loop could be used to store these in an array also so instead of the above you would have:

y 然后是数组中项目的索引,然后循环也可用于将它们存储在数组中,因此您将拥有:

z = 1
for x = 1 to length(aNames)
    if aNames(x) = sFind then 
        aIndexes(z) = x 
        z = z + 1
    endif

回答by Shimmy Weitzhandler

VB

VB

Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"}
Dim result = arr.Where(Function(a) a.Contains("ra")).Select(Function(s) Array.IndexOf(arr, s)).ToArray()

C#

C#

string[] arr = { "ravi", "Kumar", "Ravi", "Ramesh" };
var result = arr.Where(a => a.Contains("Ra")).Select(a => Array.IndexOf(arr, a)).ToArray();

-----Detailed------

- - -详细的 - - -

Module Module1

    Sub Main()
        Dim arr() As String = {"ravi", "Kumar", "Ravi", "Ramesh"}
        Dim searchStr = "ra"
        'Not case sensitive - checks if item starts with searchStr
        Dim result1 = arr.Where(Function(a) a.ToLower.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray
        'Case sensitive - checks if item starts with searchStr
        Dim result2 = arr.Where(Function(a) a.StartsWith(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray
        'Not case sensitive - checks if item contains searchStr
        Dim result3 = arr.Where(Function(a) a.ToLower.Contains(searchStr)).Select(Function(s) Array.IndexOf(arr, s)).ToArray
        Stop
    End Sub

End Module

回答by Dustin Campbell

This would do the trick, returning the values at indeces 0, 2 and 3.

这可以解决问题,返回 indeces 0、2 和 3 处的值。

Array.FindAll(arr, Function(s) s.ToLower().StartsWith("ra"))