VB.Net过滤器阵列等效于VB6

时间:2011-03-24 21:20:23

标签: vb.net arrays vb6 filter arraylist

谷歌搜索了一个小时似乎无法找到答案。以下将删除“愚蠢”并打印出“hello world”

Dim arr As Variant: arr = Array("hello", "stupid", "world")
Dim newArr As Variant: newArr = Filter(arr, "stupid", False)
Debug.Print Join(newArr, " ")

Filter的VB.Net等价物是什么?任何帮助将不胜感激!编辑:(我正在寻找2.0 .NET Framework解决方案)

2 个答案:

答案 0 :(得分:2)

或许这样的事情(假设VB 10):

Dim arr As String() = {"hello", "stupid", "world"}

Dim filteredArray = (from s in arr
                     Where s <> "stupid"
                     Select s).ToArray()

另一种方法是使用Except(可在.Net 3.5中使用):

Dim words As String() = {"hello", "stupid", "world"}
Dim excludedWords As String()  = {"stupid"}

Dim filteredArray = words.Except(excludedWords).ToArray()

答案 1 :(得分:0)

对于.NET 2.0,您可以使用Array.FindAll方法,如下所示:

Dim arr As String() = New String() {"hello", "stupid", "world"}  
Dim newArr As String() = Array.FindAll(arr, AddressOf RemoveElements)

使用此谓词:

Private Shared Function RemoveElements(ByVal s As String) As Boolean
    Return Not s.Equals("stupid")
End Function