首页 > 解决方案 > 仅删除文本文档上的某些内容 (VB.NET 4.8)

问题描述

我的问题看起来很复杂,但并不是那么复杂。我有一个文本文档,其中包含:

消除

消除

消除

消除

保持

保持

等等。为 KEEP

我想只删除 REMOVE 并保持 KEEP 没有空间。怎么做?

谢谢!

标签: vb.net

解决方案


在代码文件的顶部...

Imports System.IO

然后我们可以使用File这个命名空间中的类。请参阅https://docs.microsoft.com/en-us/dotnet/api/system.io.file?view=netcore-3.1

Private Sub RemoveRemove()
    Dim lines = File.ReadAllLines("C:\Users\maryo\Desktop\KeepRemove.txt")
    Dim KeepLines As New List(Of String)
    For Each line In lines
        If line.StartsWith("KEEP") Then
            KeepLines.Add(line)
        End If
    Next
    File.WriteAllText("C:\Users\maryo\Desktop\Keep.txt", String.Join(vbCrLf, KeepLines))
End Sub

推荐阅读