vb.net - How to find a character and replace the entire line in a text file -
how go text file find character replace entire line character on?
here's example of text file:
line1 example line2 others ...... .... id: "randomstr" more lines ...
i need find line "id"
, replace it. edited text file should be:
line1 example line2 others ...... .... "the correct line" more lines ...
first need read each line of text file, this:
for each line string in system.io.file.readalllines("pathtoyourtextfile.txt") next
next, need search string want match; if found, replace replacement value, this:
dim outputlines new list(of string)() dim stringtomatch string = "valuetomatch" dim replacementstring string = "replacementvalue" each line string in system.io.file.readalllines("pathtoyourtextfile.txt") dim matchfound boolean matchfound = line.contains(stringtomatch) if matchfound ' replace line string outputlines.add(replacementstring) else outputlines.add(line) end if next
finally, write data file, this:
system.io.file.writealllines("pathtoyouroutputfile.txt", outputlines.toarray(), encoding.utf8)
Comments
Post a Comment