如果包含特定单词,请删除整行

时间:2017-03-25 19:00:57

标签: vb.net visual-studio

我还想显示包含该单词的行。我想打开一个外部.txt文件并删除任何包含特定字符串的行。我现在搜索并替换一个单词,但希望从文件中删除整行。感谢

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myStreamReaderL1 As System.IO.StreamReader
    Dim myStream As System.IO.StreamWriter

    Dim myStr As String
    myStreamReaderL1 = System.IO.File.OpenText("C:\Users\f1r1a\Desktop\memes.txt")
    myStr = myStreamReaderL1.ReadToEnd()
    myStreamReaderL1.Close()


    myStr = myStr.Substring("fraser", 6)
    'Save myStr
    myStream = System.IO.File.CreateText("C:\Users\f1r1a\Desktop\memes.txt")
    myStream.WriteLine(myStr)
    myStream.Close()
End Sub

2 个答案:

答案 0 :(得分:1)

你可以用两行代码完成所有代码

const fs = require('fs');
require('bluebird').promisifyAll(fs);
const finder = findit("folder/");
let neededFiles = [];

finder.on("file", (file) => {

    // if file is the file content
    neededFiles.push(/* result of your op above */);
    // if file is a file name
    neededFiles.push(fs.readFile(/* file name */));
});

finder.on("error", ((error) => {});

finder.on("end", () => {
  Promise.all(neededFiles)
     .then((nf) => {
            console.log(nf); // you should now have something here...
           });
});

IEnumerable扩展其中逐行接收 File.ReadLines 生成的序列。每行都由 Where 应用 Contains 方法处理,如果该行不包含搜索的单词,则将其作为输出传递给结果变量。反过来,结果变量作为数组传递给 WriteAllLines 方法。

答案 1 :(得分:1)

你可以同时获得两个:

Dim file = "C:\Users\f1r1a\Desktop\memes.txt"

Dim lookup = File.ReadLines(file).ToLookup(Function(l) l.Contains("fraser"))

textBoxRemoved.Text = String.Join("|", lookup(True))

File.WriteAllLines(file, lookup(False))