c# - How to overwrite next line after found line in text document -
here want 1 thing, take line value, found in text document , want overwrite x not on found line, on next line coming after found line
so if content is:
line1 line2 line3 line4
and if string text = "line2";
code:
using system; using system.io; namespace _03_0 { class program { static void main(string[] args) { string text = "line2"; string text = file.readalltext("doc.txt"); text = text.replace(text, "x"); file.writealltext("doc.txt", text); } } }
i have result:
line1 x line3 line4
but want result:
line1 line2 x line4
i suggest using regular expressions this:
string filepath = @"doc.txt"; string mystr = "line2"; string content = file.readalltext(filepath); string pattern = string.format(@"(?<={0}\r\n).+?(?=\r\n)", mystr); regex r = new regex(pattern); file.writealltext(filepath, r.replace(content, "x"));
p.s: you'll need import regularexpressions
namespace:
using system.text.regularexpressions;
hope helps :)
a side note: you can't use same variable name (text
) twice.
Comments
Post a Comment