0

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

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

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

4

1 回答 1

3

您可以使用 Dictionary 来保存值:

    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)
于 2012-06-29T12:43:22.447 回答