I am reading from a file line by line and i want to edit some of the lines i read.. The lines i want to edit must have some other specific lines above or below them, but i dont know how to express that in C#. For example:
http://www.youtube.com
You Tube | Music | something
Sports
Music
Radio
Clips
http://www.youtube.com/EDIT ME
Sports
Music
Radio
Clips
and i want to edit a line only if next line is
Sports
and the previous line is
Clips
So the only line i want to edit from the example above is
http://www.youtube.com/EDIT ME
Any ideas?
You can't really "edit" a file line by line. It would be best to take one of two approaches:
File.ReadAllLines
, then make appropriate changes in memory and rewrite the whole thing (e.g. usingFile.WriteAllLines
)The first version is simpler, but obviously requires more memory. It's particularly tricky if you need to look at the next and previous line, as you mentioned.
Simple example of the first approach: