Resolved Deleting lines in a text file.

m_s_ifland

New member
Joined
Jan 28, 2010
Messages
4
Programming Experience
Beginner
Ok, I'm new so go easy on me. I don't know if this is in the right section, so if it's not, sorry.

Goal: To delete extra lines in a text file.
Example:

Current
1 blah blaah blah blaah blah blaah blah blaah blah blaah

2 blah blaah blah blaah blah blaah blah blaah

3 blah blaah blah blaah blah blaah


This is how I want it to look:
1 blah blaah blah blaah blah blaah blah blaah blah blaah
2 blah blaah blah blaah blah blaah blah blaah
3 blah blaah blah blaah blah blaah


Here is the code that I have so far.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lines() As String = {}

        Dim sr As New IO.StreamReader("C:\Documents and Settings\Michael S. Ifland\My Documents\Palm Stuff\BibleConverter\lev1.txt")
        Dim fileText As String = sr.ReadToEnd()
        lines = fileText.Split(vbCrLf)

        sr.Close()
        Dim sw As New System.IO.FileStream("C:\Documents and Settings\Michael S. Ifland\My Documents\Palm Stuff\BibleConverter\lev11.txt", IO.FileMode.Create)
        Dim w As New System.IO.StreamWriter(sw)
        Dim i As Integer
        For i = 1 To UBound(lines)
            If lines(i).Length > 1 Then
                w.Write(lines(i))
                w.Flush()
            End If
        Next
        sw.Close()

    End Sub

This code does this:
1 blah blaah blah blaah blah blaah blah blaah blah blaah 2 blah blaah blah blaah blah blaah blah blaah 3 blah blaah blah blaah blah blaah


Thanks in advance.
 
Last edited:
VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		Dim fileLines As New List(Of String)
		Using sr As New IO.StreamReader("C:\Temp\FileWithBlankLines.txt")
			fileLines.AddRange(sr.ReadToEnd.Split(Environment.NewLine))
		End Using

		fileLines = fileLines.FindAll(AddressOf FindBlanks)

		Using sw As New IO.StreamWriter("C:\Temp\FileWithoutBlanksLines.txt")
			sw.Write(String.Join(Environment.NewLine, fileLines.ToArray))
		End Using

	End Sub

	Private Function FindBlanks(ByVal line As String) As Boolean
		Return line.Trim <> ""
	End Function
 
sw.WriteLine, or for example:
VB.NET:
Dim lines = IO.File.ReadAllText("lev.txt").Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
IO.File.WriteAllLines("lev2.txt", lines)
 
Modification: the code above does exactly what I asked. Thank you.
I was wondering if you could help me modify it some more.

Example:

1 I went to the store to buy some
milk and some bread.
2 The reason I did this was because of the winter
storm watch.


What I'm looking for is if there is not a number in front of the sentence then the sentence will be moved to the line above.

Goal:

1 I went to the store to buy some milk and some bread.
2 The reason I did this was because of the winter storm watch.


Thanks for your help.
 
VB.NET:
Dim build As New System.Text.StringBuilder(lines(0))
For i As Integer = 1 To lines.Length - 1
    Dim line As String = lines(i)
    If Char.IsDigit(line(0)) Then build.Append(vbNewLine)
    build.Append(line)
Next
IO.File.WriteAllText("newlines.txt", build.ToString)
Append a space also if the lines are trimmed.
An option is to write directly to file with StreamWriter instead of StringBuilder.
 
Back
Top