Delete double lines in a line

I am developing an asp.net web application, I have a line (with the value in it from the database), with several lines that I insert into a TextBox with type mulitline. (Text field)

Now the problem is that there are several lines in the row, with lots of empty space. so I want to remove only double lines.

example of my text box:

++++++++++++++++++++++++++++++++++++++++++++++++++ +++ +++
{Empty}
{Empty}
"This is the text in the text box 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 delete lines 1 and 2, and lines 7 and 8

early

+3
source share
4 answers

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 was tested OK with Visual Studio 2010.

+1
source

This will save you from all blank lines.

    Dim splt() As Char = New Char() {ControlChars.Lf, ControlChars.Cr}
    Dim lines() As String = TextBox1.Text.Split(splt, StringSplitOptions.RemoveEmptyEntries)
    TextBox1.Lines = lines

,

    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
0

, , , 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)
0

The following code deletes double blank lines at the beginning, as well as double blank lines anywhere in the text field.

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 above example, it will delete lines 1 and 2, as well as lines 7 and 8.

0
source

Source: https://habr.com/ru/post/1786897/


All Articles