Hello Guys.

Consider the following:

I have a text document that has a lot of data. I want to search for a specified term say "apple" and remove that entire line of data from the document.

Thanks.

I would create a file reader and read the text document line by line. Treat each line as a single string. Use the String.Replace method to replace. I would then write the new line to another new text file. example:

Dim strFileName as string = "C:\myfile.txt"
Dim strNewFileName as string = "C:\MyNewFile.txt"
 Dim oReader As New StreamReader(strFileName)
Dim oWriter as New StreamWriter(strFileName, False)
 Dim strLine As String
 strLine = oReader.ReadLine
 Do While oReader.Peek >= 0
    strLine = strLine.Replace("Apple", "Orange")
   oWriter.WriteLine(strLine)
   strLine = oReader.Readline
Loop

To remove entire line just don't write the offending line to the new file:

Dim strFileName as string = "C:\myfile.txt"
Dim strNewFileName as string = "C:\MyNewFile.txt"
 Dim oReader As New StreamReader(strFileName)
Dim oWriter as New StreamWriter(strFileName, False)
 Dim strLine As String
 strLine = oReader.ReadLine
 Do While oReader.Peek >= 0
   If strLine.Contains("Apple") = false then oWriter.WriteLine(strLine)
   strLine = oReader.Readline
Loop

Hello Guys.

Consider the following:

I have a text document that has a lot of data. I want to search for a specified term say "apple" and remove that entire line of data from the document.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.