编辑我的文本文件只显示最高值?

时间:2012-06-29 12:33:41

标签: .net vb.net vb.net-2010

我使用以下编码根据列中的值小于文本框中的值来编辑我的文本文件。

Dim intValue As Integer
        Dim intMaxValue As Integer = Integer.Parse(textbox1.Text)

        Dim strSourceFile As String = IO.Path.Combine("G:\test.txt")
        Dim OutPutFile As String = IO.Path.Combine("G:\test2.txt")

        Dim strLines() As String = IO.File.ReadAllLines(strSourceFile)
        Dim strFiltered As New System.Text.StringBuilder
        Dim strTemp() As String

        For Each strLine In strLines
            If strLine.Trim.Length <> 0 Then
                strTemp = strLine.Split(" "c)
                If Trim(strTemp(0)) = "USER" AndAlso Trim(strTemp(2)) = "1" Then
                    strLine = strTemp(8).Trim & " " & strTemp(16).Trim
                    If Integer.TryParse(strLine.Split(" "c)(1), intValue) Then
                        If intValue <= intMaxValue Then
                            strFiltered.Append(strLine & Environment.NewLine)
                        End If
                    End If
                End If
            End If

        Next

        IO.File.WriteAllText(OutPutFile, strFiltered.ToString)

现在上面的编码工作正常,输出如下: -

String1 100
String1 256
String1 500
String2 100
String2 256
String3 876
String3 345
String3 643
String3 102
String4 100
String4 084
String5 492
String5 178
String6 873
String6 156
String6 786

我所希望的是添加额外的编码所以我只想要显示最高编号的字符串,以便上面看起来像

String1 500
String2 256
String3 876
String4 100
String5 492
String6 873

是否可以添加编码的最后一位?

更新

我希望检查第1列中的每个匹配字段并检查最高编号,如果它大于textbox1中的字段,则删除第1列中匹配字段的所有行,而不是检查最高编号并删除其他字段。 。如果最高数字低于textbox1中的字段,则保留该行但删除其他列1匹配字段。例如,

String1 100
String1 256
String1 500
String2 100
String2 256
String3 876
String3 345
String3 643
String3 102
String4 100
String4 084
String5 492
String5 178
String6 873
String6 156
String6 786

因此,如果textbox1有550,那么你应该

String1 500
String2 256
String4 100
String5 492

更新2

textbox1中的值为

1341273599

当我只过滤列以显示第1列和第2列时,我得到以下内容。

S00048 1428142557
S00048 1428141809
S00048 1338805621
S00048 1310295931
S00048 1309086124
S00048 1432203954
S00048 1431686625
S00048 1428142556
S00048 1431686626
S00048 1334743408
S00042 1324204635
S00040 1313659927
S00037 1308388943
S00033 1303118141
S00032 1391422317
S00032 1391422304
S00032 1298024019
S00032 1391422303
S00032 1391422316

因此,当我运行实际编码时,我得到以下内容

S00048 1338805621
S00042 1324204635
S00040 1313659927
S00037 1308388943
S00033 1303118141
S00032 1298024019

您可能会看到最终结果不正确?

1 个答案:

答案 0 :(得分:3)

您可以使用词典来保存值:

    Dim FilteredDictionary as new Dictionary(string, integer)

    Dim intValue As Integer
    Dim intMaxValue As Integer = Integer.Parse(textbox1.Text)

    Dim strSourceFile As String = IO.Path.Combine("G:\test.txt")
    Dim OutPutFile As String = IO.Path.Combine("G:\test2.txt")

    Dim strLines() As String = IO.File.ReadAllLines(strSourceFile)
    Dim strFiltered As New System.Text.StringBuilder
    Dim strTemp() As String

    Dim lastIntValue as integer = 0
    For Each strLine In strLines
        If strLine.Trim.Length <> 0 Then
            strTemp = strLine.Split(" "c)
            If Trim(strTemp(0)) = "USER" AndAlso Trim(strTemp(2)) = "1" Then
                strLine = strTemp(8).Trim & " " & strTemp(16).Trim
                If Integer.TryParse(strLine.Split(" "c)(1), intValue) Then
                      If intValue = 0 Then
                        intValue=lastIntValue
                      Else
                        lastIntValue=intValue
                      End If
                      If FilteredDictionary.ContainsKey(strTemp(8).Trim) then
                        If intValue > FilteredDictionary(strTemp(8).Trim) then
                          FilteredDictionary(strTemp(8).Trim) = intValue
                        End If
                      Else
                        FilteredDictionary.Add(strTemp(8).Trim, intValue)
                      End If
                        'strFiltered.Append(strLine & Environment.NewLine)

                End If
            End If
        End If
    Next

    'Modifed stringbuilder to only add those items that are less than or equal to
    'a given value in Textbox1.  Note that if Textbox1 is not an integer,
    'it will throw an error.  You could use Integer.TryParse instead.
    For Each item As String in FilteredDictionary.Keys.ToList
       If FilteredDictionary(item) <= Convert.ToInt32(Textbox1.Text)
         strFiltered.AppendLine(item & " " & FilteredDictionary(item))
       EndIf
    Next
    IO.File.WriteAllText(OutPutFile, strFiltered.ToString)