在文本文件中搜索特定值

时间:2013-11-11 10:36:16

标签: vb.net list loops split

我必须列出许多变种。第一个清单的样本

列表1)

hkdhksa OP-ID: 111112 jklfjdlkfsd hfldhfjksdf OP-ID: 111113 ghjg  
    OP-ID: 111114  OP-ID: 111115 gjgjhghgjhg OP-ID: 111116 OP-ID: 111117 OP-ID: 111118

清单2)

OP-ID: 111112 OP-ID: 11113 OP-ID: 111114 OP-ID: 111115 OP-ID: 111117

结果将是:OP-ID:11118不在列表2中

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click

    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 line = ("OP-ID: ") Like "OP-ID:*" Then
            If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)
        End If

    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


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

    txtResults.Text = ("IDs should not be in list: " & str)
End Sub

0 个答案:

没有答案