9

Basically I have a text file that I read in and display in a rich text box, which is fine, but I then want to be able to search through the text for a specific word and delete the whole line of text that contains this word. I can search through the text to see if the word exists or not but I cannot figure out how to delete the whole line. Any help would be great.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1364063
  • 93
  • 1
  • 1
  • 3

4 Answers4

19

The easiest is to rewrite the whole file without the line(s) that contain the word. You can use LINQ for that:

var oldLines = System.IO.File.ReadAllLines(path);
var newLines = oldLines.Where(line => !line.Contains(wordToDelete));
System.IO.File.WriteAllLines(path, newLines);

If you only want to delete all lines that contain the word(not only the sequence of characters), you need to split the line by ' ':

var newLines = oldLines.Select(line => new { 
            Line = line, 
            Words = line.Split(' ') 
        })
        .Where(lineInfo => !lineInfo.Words.Contains(wordToDelete))
        .Select(lineInfo => lineInfo.Line);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
7

You can do it easily without LINK

                string search_text = text;
                string old;
                string n="";
                StreamReader sr = File.OpenText(FileName);
                while ((old = sr.ReadLine()) != null)
                {
                    if (!old.Contains(search_text))
                    {
                        n += old+Environment.NewLine;  
                    }
                }
                sr.Close();
                File.WriteAllText(FileName, n);
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
  • @mMd Kamruzzaman Pallob how can i do for multiple words.. this worked perfectly for me.. But for a single word.. I need to look for three more words.. How can i do this.. Thanks – Stacy Kebler Nov 03 '14 at 18:31
2

Code:

"using System.Linq;" is required.

Write your own extension method IsNotAnyOf(,) (put it in a static class) and call the method (i. e. it is called) from .Where(n => n.IsNotAnyOf(...))...(); The for-loop will return false if the condition is met, if not the method will return true:

static void aMethod()
{
    string[] wordsToDelete = { "aa", "bb" };
    string[] Lines = System.IO.File.ReadAllLines(TextFilePath)
        .Where(n => n.IsNotAnyOf(wordsToDelete)).ToArray();
    IO.File.WriteAllLines(TextFilePath, Lines);
}

static private bool IsNotAnyOf(this string n, string[] wordsToDelete)
{    for (int ct = 0; ct < wordsToDelete.Length; ct++)
         if (n == wordsToDelete[ct]) return false;
     return true;
}
Community
  • 1
  • 1
1

We can convert the string to an array of lines, work over it, and convert back:

string[] dados_em_lista = dados_em_txt.Split(
                        new[] { "\r\n", "\r", "\n" },
                        StringSplitOptions.None
                    );

var nova_lista = dados_em_lista.Where(line => !line.Contains(line_to_remove)).ToArray();

dados_em_txt = String.Join("\n", nova_lista);
Pedro Reis
  • 1,587
  • 1
  • 19
  • 19