0
Public Class Form1

    Const TEST1 = "\\folder\compare\list1.txt"
    Const TEST2 = "\\folder\compare\list2.txt"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)       Handles Button1.Click
    'Declare two dictionaries. The key for each will be the text from the input line up to,
    'but not including the first ",". The valus for each will be the entire input line.

    Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",") 
        file1.Add(part(0), line)

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        file2.Add(part(0), line)
    Next

    AddText("The following lines from " & TEST2 & " are also in " & TEST1)

    For Each key As String In file2.Keys
        If file1.ContainsKey(key) Then
            AddText(file2(key))
        End If
    Next

End Sub

Private Sub AddText(ByVal text As String)
     txtResults.Text &= text & vbCrLf
End Sub

列表 1)12345
列表 2)1234
结果:5 不在列表 1 中

我想比较 vb.net 中的两个文本文件以检查是否有不在列表一中的值 我面临的问题是列表比较所有匹配的值,但我还想要一个不匹配的值列表。我想显示列表 1 中不在列表 2 中的值。

4

2 回答 2

0

在你的 for 循环中,放入一个else并将值放入另一个列表中:

For Each key As String In file2.Keys
    If file1.ContainsKey(key) Then
        AddText(file2(key))
    Else
        ...Add to anther list or string here
    End If
Next
于 2013-11-07T21:37:21.407 回答
0

简单的,

Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys).ToList

假设 list1 is-superset-oflist2。否则只需交换它们。

编辑

打印它,

Dim values = From key In keysInList1ThatAreNotInList2  Select file1(key)
Dim str = String.Join(vbCrLf, values)
AddText(str)
于 2013-11-08T09:12:41.343 回答