在文本文件中搜索特定值

时间:2013-11-11 11:09:06

标签: vb.net

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

清单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
    '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 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

2 个答案:

答案 0 :(得分:0)

当您只处理一个字段时,不确定为什么要使用字典。每个值都具有相同的键,字典必须具有唯一键。一个简单的列表(字符串)应该运行良好:

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Dim file1 As New List(Of String)
    Dim file2 As New List(Of String)
    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        If line.StartsWith("OP-ID:") Then
            Dim temp As String = line.Split(" "c)(1)
            If Not file1.Contains(temp) Then
                file1.Add(temp)
            End If
        End If
    Next
    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        If line.StartsWith("OP-ID:") Then
            Dim temp As String = line.Split(" "c)(1)
            If Not file2.Contains(temp) Then
                file2.Add(temp)
            End If
        End If
    Next
    Dim keysInList1ThatAreNotInList2() As String = file1.Except(file2).ToArray
    Dim str = String.Join(vbCrLf, keysInList1ThatAreNotInList2)
    txtResults.Text = ("IDs should not be in list: " & vbCrLf & str)
End Sub

答案 1 :(得分:0)

您的样本列表中没有逗号,并且问题是否存在也不清楚。

如果没有逗号

如果TEST1中没有重复项(或您不关心它们):

Dim hs As New Hashset(Of String)(File.ReadLines(TEST1))
hs.ExceptWith(File.ReadLines(TEST2))
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, hs)}"

如果重复是重要的:

Dim hs As New Hashset(Of String)(File.ReadLines(TEST2))
Dim notFound = File.ReadLines(TEST1).Where(Function(x) Not hs.Contains(x)).ToList
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果有逗号

(共享代码)

Dim keyParser = Function(x As String)
        Dim indexOf = s.IndexOf(
        If indexOf = -1 Then Return x
        Return Left(x, indexOf)
    End Function
Dim list2Keys = New Hashset(Of String)(File.ReadLines(TEST2).Select(keyParser))

如果TEST1中没有重复的密钥:

Dim list1 = File.ReadLines(TEST1).ToDictionary(keyParser)
Dim notFound = list1.Where(Function(kvp) Not list2Keys.Contains(kvp.Key)).Select(Function(kvp) kvp.Value)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果TEST1中有重复的密钥,但它们无关紧要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).Select(Function(grp) grp.First)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果TEST1中的重复键很重要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).SelectMany(Function(grp) grp)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"