How to compare two arrays of string?

前端 未结 3 1538
陌清茗
陌清茗 2021-01-16 13:21

I want to compare two string arrays. I don\'t know how to implement this in vb.net.

Let\'s say I have two arrays

Dim A() As String = {\"Hello\", \"H         


        
3条回答
  •  执笔经年
    2021-01-16 13:49

    Use Linq extensions. Except will tell you which items are different

    ' ones in A but not in B
    Dim result() As String = A.Except(B).ToArray()
    
    ' ones in B but not in A
    Dim result() As String = B.Except(A).ToArray()
    

    And you can use Join to find same items

    Here is One method to find same items, or different items

    Dim a() as string = {"a", "b", "c"}
    Dim b() as string = {"a", "b", "z"}
    
    Dim sameItems() as String = a.Where(Function(i) b.Contains(i)).ToArray()
    Array.ForEach(sameItems, Sub(i) Console.WriteLine(i))    
    
    Dim diffItems() as String = a.Where(Function(i) Not b.Contains(i)).ToArray()
    Array.foreach(diffItems, Sub(i) Console.WriteLine(i))    
    

    And of course, you can use Intersect

提交回复
热议问题