0

我想在文本文件中搜索另一个文本文件中包含的值。结果显示列表 2 中未包含的值

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 HashSet(Of String) '!
    '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(",")
        If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        If Not file2.ContainsKey(part(0)) Then 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.Contains(key) Then
            AddText(file2(key))
        End If
    Next
    Dim keysInList1ThatAreNotInList2 = file1.Except(file2.Keys).ToList '!



    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)
    AddText("ID should not be in this list" & str)

    End Sub

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

End Class

我想在文本文件中搜索另一个文本文件中包含的值。结果显示列表 2 中未包含的值。目前,只要这些值是唯一的,它就可以工作。我想在大型文本文件中搜索特定值。

4

1 回答 1

0

该解决方案似乎只寻找第一次出现,因此在创建 file1、file2 时有必要忽略重复

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(",") 
        If Not file1.ContainsKey(part(0)) Then  file1.Add(part(0), line)'!

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",") 
        If Not file2.ContainsKey(part(0)) Then  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
    Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys).ToList

    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)
    AddText(str)

End Sub

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

注意为避免混淆,我已删除原始答案。这是新的完整的。

于 2013-11-11T09:16:02.697 回答