Home > database >  How do I remove multiple empty lines in a text file
How do I remove multiple empty lines in a text file

Time:10-23

I wonder if someone is able to help. I have a m3u file with multiple lines of formatted text.

#EXTM3U
#RADIOBROWSERUUID:963194ef-0601-11e8-ae97-52543be04c81
#EXTINF:1,80s80s Christmas
http://streams.80s80s.de/christmas/mp3-192/streams.80s80s.de/

#RADIOBROWSERUUID:9638cfa5-0601-11e8-ae97-52543be04c81
#EXTINF:1,181.FM - Christmas Kountry
http://www.181.fm/stream/pls/181-xkkountry.pls

Whilst I have managed to extract the data into a format that I can need... I am left with multiple blank lines. A sample bit of code I used to Extract the data is...

                If line.StartsWith("#EXTM3U") Then 'Remove
                    lines(i) = ""
                End If
                If line.StartsWith("#RADIOBROWSERUUID:") Then 'Remove
                    lines(i) = ""
                End If
                If line.StartsWith("#EXTINF:1,") Then 'Add # at beginning of line
                    lines(i) = line.Replace("#EXTINF:1,", "#") 
                End If

Which then leaves me with the following...

#80s80s Christmas
@http://streams.80s80s.de/christmas/mp3-192/streams.80s80s.de/


#181.FM - Christmas Kountry
@http://www.181.fm/stream/pls/181-xkkountry.pls

I just dont seem to be able to remove the empty/blank lines. I have used google as well as here and non of the answers seem to work for me. Here is the code that I am using...

Dim Newlines As String() = File.ReadAllLines(ofd.FileName)

For t As Integer = 0 To Newlines.Length - 1
Dim line2 As String = Newlines(t)
If line2.StartsWith("") Then ' Remove blank lines
Beep()
Newlines(t) = line2.Replace(Environment.NewLine, String.Empty)  
End If
Next

File.WriteAllLines("NewTextm3u.txt", lines)

Can any body see where I am going wrong? Thank you very much.

CodePudding user response:

You can do this:

   Dim sFile As String = "c:\test2\test2.txt"

    Dim Fdata As New List(Of String)

    Fdata = File.ReadAllLines("c:\test2\test.txt").ToList

    For i = Fdata.Count - 1 To 0 Step -1
        If Fdata(i) = "" Then
            Fdata.RemoveAt(i)
        End If
    Next

    For Each sLine As String In Fdata
        Debug.Print(sLine)
    Next

    File.WriteAllLines(sFile)

The above would remove all blank lines

In place of that loop, you could also use lamda expression like this:

    Fdata.RemoveAll(Function(MyOneRow) MyOneRow = "")

CodePudding user response:

I used the StrignSplitOptions.RemoveEmptyEntries to get rid of blank lines.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim FileContents = File.ReadAllText("SomeFile.txt")
    Dim lines = FileContents.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
End Sub

CodePudding user response:

enter image description here

As you can see in the above image, there is what we can call an "enter" at the end of the sentence and another one between. So let's just remove the one between the sentences and because it's just a line you can do it like this:

If line(i) = CHR(13) & CHR(10) then line(i)=""

but if you want to get a little paranoid and just want to remove all the "enters" or "line breaks" just do it like this:

 line(i)=Replace(line(i),CHR(13) & CHR(10),"")
  • Related