dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] If line contains "word" then delete full line.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    8

    Resolved [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

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Location
    Buckingham, UK
    Posts
    260

    Re: If line contains "word" then delete full line.

    Quote Originally Posted by mmmx19 View Post
    ... 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

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,144

    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"))

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,144

    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)

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,723

    Re: If line contains "word" then delete full line.

    Quote Originally Posted by .paul. View Post
    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).

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,440

    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!

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,796

    Re: If line contains "word" then delete full line.

    Quote Originally Posted by .paul. View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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
  •  



Featured


Click Here to Expand Forum to Full Width