|
-
Nov 24th, 2019, 02:37 PM
#1
Thread Starter
New Member
[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
-
Nov 24th, 2019, 02:46 PM
#2
Hyperactive Member
Re: If line contains "word" then delete full line.
 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
-
Nov 24th, 2019, 03:00 PM
#3
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"))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2019, 03:04 PM
#4
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2019, 05:27 PM
#5
Re: If line contains "word" then delete full line.
 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).
-
Nov 25th, 2019, 06:55 AM
#6
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
Please remember next time...elections matter!
-
Nov 26th, 2019, 08:54 AM
#7
Re: If line contains "word" then delete full line.
 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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|