[RESOLVED] If line contains "word" then delete full line.
Hi, so like in title. Is there any way to delete line If it contains a "word"?
Ex.
Code:
Dim text As String = File.ReadAllText("index.html")
"text" is my full text. And i want to detect line with "word" and delete this line.
Thank you in advance for your help
Re: If line contains "word" then delete full line.
Quote:
Originally Posted by
mmmx19
... like in title. Is there any way to delete line If it contains a "word"?
Code:
Dim text As String = File.ReadAllText("index.html")
"text" is my full text. And i want to detect line with "word" and delete this line.
What do you mean by "line"? - File.ReadAllText will read (and hence your variable "text" will contain) the contents of the entire file that has been read.
Kind Regards, John
Re: If line contains "word" then delete full line.
As JohnW2 told you, text is all text, lines are lines in the file, and the easiest way to remove lines is if you read them into a list(of string) and use removeall on that list...
Code:
Dim lines As New List(of String)(File.ReadAlllines("index.html"))
lines.removeall(function(l) l.contains("word"))
Re: If line contains "word" then delete full line.
You should note, that'll remove all lines that contain "word", not just the first instance.
To write it back to your file use...
Code:
IO.File.WriteAllLines("filename", lines.ToArray)
Re: If line contains "word" then delete full line.
Quote:
Originally Posted by
.paul.
You should note, that'll remove all lines that contain "word", not just the first instance.
To write it back to your file use...
Code:
IO.File.WriteAllLines("filename", lines.ToArray)
The OP specifies VB 2017, so no need for that ToArray call. WriteAllLines will accept any IEnumerable(Of String).
Re: If line contains "word" then delete full line.
Something like:
Code:
Imports System.IO
Dim Lines = File.ReadAllLines("C:\MyText.txt")
Outputfile = My.Computer.FileSystem.OpenTextFileWriter("C:\MyText.txt", False)
For Each line In Lines
If InStr(1, UCase(line), UCase("Word")) = 0 Then
Outputfile.WriteLine(line)
End If
Next
Re: If line contains "word" then delete full line.
Quote:
Originally Posted by
.paul.
You should note, that'll remove all lines that contain "word", not just the first instance.
Not only that but it'll delete lines that contain words that contain "word" ... like "crossword"
-tg