简体   繁体   English

Streamwriter,删除找到特定字符的整行

[英]Streamwriter, remove whole line where a specific character is found

Maybe I'm overthinking this. 也许我想得太多了。 I've got a working method for writing to a text file, and reading from it. 我有一个写文本文件并从中读取的有效方法。

Writing: 写作:

public void SaveNotes()
{
    using (StreamWriter save= new StreamWriter("notes.txt", true))
    {               
        foreach (var element in collection)
        {
            spara.Write(element.Prio+ " " + element.Note+ "\n");
        }
    }
}

Now I'm trying to design a method that looks for any '0' in my list, and overwrite the entire line. 现在,我正在尝试设计一种方法,该方法在列表中查找任何'0'并覆盖整行。

Is there any easier way to tell the program that all lines containing a '0' should be overwritten with blankspaces? 有没有更简单的方法来告诉程序所有包含'0'行都应该用空格覆盖? Any input much appreciated. 任何输入非常感谢。

public static void Delete(Note note)
{         
    using (StreamWriter remove = new StreamWriter("notes.txt"))
    {
        Regex rgx = new Regex("");
        foreach (var element in collection)
        {       
            while (element.Notering.Contains("0"))
            {
                rgx.Replace(element.Note, "");   
                rgx.Replace(element.Prio, "");
            }
        }
    }
}

Why don't you combine two of them? 您为什么不将两者结合?

public void SaveNotes()
{
    using (StreamWriter save= new StreamWriter("notes.txt", true))
    {               
        foreach (var element in collection)
        {
            if (element.Prio.IndexOf('\0') < 0 && element.Note.IndexOf('\0') < 0)
                spara.Write(element.Prio+ " " + element.Note+ "\n");
            else spara.Write(" \n");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM