remove blank lines from textbox

TyE14

New member
Joined
Aug 8, 2007
Messages
4
Programming Experience
Beginner
All I want to do is make it so that when you click a button, the text in one textbox appears in another textbox with all the blank lines deleted. for example this is whats in textbox1:

1

dd


sss


s

d

and when I click a button I want this to appear in textbox2:

1
dd
sss
s
d

All I want to do this is with a form, a button, and 2 textboxes. please help.
 
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim lines() As String = TextBox1.Text.Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
    TextBox2.Text = String.Join(vbNewLine, lines)
End Sub
 
This proved to be a bit more complicated that I thought. I thought it would be as easy as replacing all the vbcrlf's but that would take out the line breaks that you actually want. Instead I had to handle this a bit different. You can take a look at how I accomplished this. It may have a bug or two but you should be able to work out the kinks and put actually put it in your application.

My application had two multiline text box's (txtStart, txtResult) next to one another an button. When the button is clicked it will remove the unwanted line breaks in your text. The following should accomplish what you want.

VB.NET:
     Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        Dim text As String = txtStart.Text

        'loop through all the chars in the textbox
        For x As Integer = 0 To text.Length - 1
            'is the character a special character?
            If Char.IsControl(text.Chars(x)) Then
                'is this character and the next character a CrLf combination
                If text.Chars(x) = ControlChars.Cr AndAlso text.Chars(x + 1) = ControlChars.Lf Then
                    'to prevent an out of range exception at the end of the loop
                    If x + 2 <= text.Length - 1 Then
                        'to find out if we want to keep what is after the CrLf
                        If Char.IsControl(text.Chars(x + 2)) = False Then
                            'we want to keep the info after the line break
                            txtResult.AppendText(vbCrLf)
                        End If
                    End If
                End If
            Else
                'output to result textbox because character is not a controlchar
                txtResult.AppendText(text.Chars(x))
            End If
        Next



    End Sub

Actually the above response is a much better way to go about it I am sure. There are always many ways to do something as you can see, especially in the .NET framework.
 
Here's also a different approach that would be more useful in some cases, for example to also get rid of lines that contain some spaces:
VB.NET:
Dim linelist As New List(Of String)
For Each line As String In TextBox1.Lines
    If line.Trim <> "" Then linelist.Add(line)
Next
TextBox2.Lines = linelist.ToArray
 
This proved to be a bit more complicated that I thought.


As a one liner:

s = Regex.Replace(YourFileAsString, "[\r\n][\r\n ]{2,}", Environment.NewLine)


"Replace all occurrences of CR or LF appearing as a block of 3 or more in the input"



This should also deal with lines that look blank because they contain only spaces
 
Back
Top