How to compare two arrays of string?

前端 未结 3 1573
陌清茗
陌清茗 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:52

    With Linq use Except() to find the differences between the two arrays and Intersect() to find which elements are in both arrays.

    Imports System.Linq
    
    Module Module1
        Sub Main()
            Dim A() As String = {"Hello", "How", "Are", "You?"}
            Dim B() As String = {"You", "How", "Something Else", "You"}
    
            Console.WriteLine("A elements not in B: " + String.Join(", ", A.Except(B)))
            Console.WriteLine("B elements not in A: " + String.Join(", ", B.Except(A)))
            Console.WriteLine("Elements in both A & B: " + String.Join(", ", A.Intersect(B)))
    
            Console.ReadLine()
        End Sub
    End Module
    

    Results:

    A elements not in B: Hello, Are, You?
    B elements not in A: You, Something Else
    Elements in both A & B: How
    

提交回复
热议问题