【问题标题】:Remove Repeated Words from Text file从文本文件中删除重复的单词
【发布时间】:2016-07-31 04:36:37
【问题描述】:

我有一个文本文件,包含近 45,000 个单词,每行一个单词。数以千计的这些词出现超过 10 次。我想创建一个没有重复单词的新文件。我使用了 Stream 阅读器,但它只读取一次文件。我怎样才能摆脱重复的话。请帮我。谢谢 我的代码是这样的

Try
        File.OpenText(TextBox1.Text)
    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try

    Dim line As String = String.Empty
    Dim OldLine As String = String.Empty
    Dim sr = File.OpenText(TextBox1.Text)

    line = sr.ReadLine
    OldLine = line

    Do While sr.Peek <> -1
        Application.DoEvents()
        line = sr.ReadLine
        If OldLine <> line Then
                My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Splitted File without Repeats.txt", line & vbCrLf, True)
        End If

        OldLine = line
    Loop


    sr.Close()
    System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Splitted File without Repeats.txt")
    MsgBox("Loop terminated. Stream Reader Closed." & vbCrLf)

【问题讨论】:

    标签: vb.net file text words


    【解决方案1】:

    您可以为此使用 LINQ 的 Distinct() 方法。

    这适用于较小的文件:

    Dim lines As String() = File.ReadAllLines("yourfile.txt")
    File.WriteAllLines("yourfile.txt", lines.Distinct().ToArray())
    

    【讨论】:

    • 当然亲爱的拉基蒂奇。你应该得到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多