Reputation: 27
I am developing a asp.net web application, i have a string (with a value in it from a database), with multiple lines that i put in a TextBox with mulitline type. (textarea)
Now the problem is, that in the string are multiple lines, with much empty space. so i want the remove only the double linebreaks.
example of my textbox:
+++++++++++++++++++++++++++++++++++++++++++++++++++++
{empty}
{empty}
'This is some text in the textbox on line 3
'some text on line 4
{empty}
'some text on line 6
{empty}
{empty}
'some text on line 9
{empty}
+++++++++++++++++++++++++++++++++++++++++++++++++++++
now somehow i want to remove line 1 and 2, and line 7 and 8
thanks in advance
Upvotes: 3
Views: 2577
Reputation: 1275
Here is the solution:
'now rebuild your example string
Dim Empty As String = Chr(13) & Chr(10)
Dim Sb As New System.Text.StringBuilder
Sb.Append("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append(Empty & "This is some text in the textbox on line 3")
Sb.Append(Empty & "some text on line 4")
Sb.Append(Empty)
Sb.Append(Empty & "some text on line 6")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append(Empty & "some text on line 9")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Dim YourString As String = Sb.ToString
MessageBox.Show(YourString)
'now replace the double empty
Dim result As String
result = YourString.Replace(Empty & Empty & Empty, Empty)
MessageBox.Show(result)
NOTE: This solution has been tested OK with Visual Studio 2010.
Upvotes: 1
Reputation: 3461
The following code removes double empty lines at the beginning, and also double empty lines anywhere in the textbox.
Dim myText as String = TextBox1.Text
myText = Regex.Replace(myText, "^(\r\n\r\n)(.*)", "$2")
myText = Regex.Replace(myTextt, "(.*\r\n)(\r\n\r\n)(.*)", "$1$3")
TextBox1.Text = myText
In the example given, it would remove lines 1 and 2, and lines 7 and 8.
Upvotes: 0
Reputation: 55417
The way I usually do this is to convert all of the various line breaks into a single one that I can manage, de-dupe and convert back to vbNewLine:
'//Convert all line break types to vbCr/ASCII 13
T = T.Replace(vbNewLine, vbCr).Replace(vbLf, vbCr)
'//Loop until all duplicate returns are removed
Do While T.Contains(vbCr & vbCr)
T = T.Replace(vbCr & vbCr, vbCr)
Loop
'//Check to see if the string has one at the start to remove
If T.StartsWith(vbCr) Then T = T.TrimStart(Chr(13))
'//Convert back to standard windows line breaks
T = T.Replace(vbCr, vbNewLine)
Upvotes: 0
Reputation: 11773
This will get rid of all empty lines.
Dim splt() As Char = New Char() {ControlChars.Lf, ControlChars.Cr}
Dim lines() As String = TextBox1.Text.Split(splt, StringSplitOptions.RemoveEmptyEntries)
TextBox1.Lines = lines
This looks like it will get rid of multiple newlines
Dim s As String = TextBox1.Text.Replace(Environment.NewLine, ControlChars.Cr)
Dim lines As New List(Of String)
lines.AddRange(s.Split(New Char() {ControlChars.Cr}))
For x As Integer = lines.Count - 1 To 1 Step -1
If lines(x) = "" AndAlso lines(x - 1) = "" Then
lines.RemoveAt(x)
End If
Next
TextBox1.Lines = lines.ToArray
Upvotes: 0