在一个文本文件中搜索另一个文本文件中的值

时间:2013-11-08 14:13:00

标签: vb.net

我想在文本文件中搜索另一个文本文件中包含的值。结果显示未包含在列表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中未包含的值。我想在一个大文本文件中搜索特定的值。

1 个答案:

答案 0 :(得分: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

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