Home > Enterprise >  textbox multiline delete all rows starting with a certain character
textbox multiline delete all rows starting with a certain character

Time:10-20

I'd like to delete in a textbox multiline, all rows that starts with a char different from 1-9 numbers, in other terms, I'd like to delete all rows starting with a-z A-Z and 0. I made several tests thinking it was easy, but I didn't solved yet.

It seems that's not so easy to handle a textbox multiline normally.
This is the last code that I tried without success.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim lines() As String
    lines = TextBox1.Lines

    For Each line As String In lines
        If line.Length >= 1 Then
            Dim firstLetter As Char
            firstLetter = line.Substring(0, 1)

            If firstLetter = "S" Then
                TextBox1.Lines.ToArray
            End If
        End If
    Next
End Sub

CodePudding user response:

There are a number of variations but the simplest and most succinct is to use LINQ:

Dim lines = From line In TextBox1.Lines
            Let firstChar = line(0)
            Where Char.IsDigit(firstChar) AndAlso firstChar <> "0"c
            Select line

TextBox1.Lines = lines.ToArray()

Firstly, this code assumes that every line contains at least one character. If that's not the case then you'd have to make the appropriate adjustment.

The Let keyword allows you to create a local variable within a LINQ query, which is handy here because it allows us to use the same value multiple times without using a complex expression to get that value multiple times.

The filter first gets the lines that start with a digit, then excludes the ones that start with a '0'.

Alternatively, you can use a conventional loop:

Dim lines = TextBox1.Lines.ToList()

For i = lines.Count - 1 To 0 Step -1
    Dim firstChar = lines(i)(0)

    If firstChar = "0"c OrElse Not Char.IsDigit(firstChar) Then
        lines.RemoveAt(i)
    End If
Next

TextBox1.Lines = lines.ToArray()

You need to create a collection from the Lines array, so you can remove items. As is always the case, if you want to modify the collection as you loop, you need to loop backwards, so changes don't affect the index of items yet to be processed.

If you might have blank lines and you want to exclude them as well, these will work:

Dim lines = From line In TextBox1.Lines
            Let firstChar = line.FirstOrDefault()
            Where Char.IsDigit(firstChar) AndAlso firstChar <> "0"c
            Select line

TextBox1.Lines = lines.ToArray()
Dim lines = TextBox1.Lines.ToList()

For i = lines.Count - 1 To 0 Step -1
    Dim firstChar = lines(i).FirstOrDefault()

    If firstChar = "0"c OrElse Not Char.IsDigit(firstChar) Then
        lines.RemoveAt(i)
    End If
Next

TextBox1.Lines = lines.ToArray()

If you want to retain blank lines:

Dim lines = From line In TextBox1.Lines
            Let firstChar = line.FirstOrDefault()
            Where firstChar = ControlChars.NullChar OrElse (Char.IsDigit(firstChar) AndAlso firstChar <> "0"c)
            Select line

TextBox1.Lines = lines.ToArray()
Dim lines = TextBox1.Lines.ToList()

For i = lines.Count - 1 To 0 Step -1
    Dim firstChar = lines(i).FirstOrDefault()

    If firstChar = "0"c OrElse (firstChar <> ControlChars.NullChar AndAlso Not Char.IsDigit(firstChar)) Then
        lines.RemoveAt(i)
    End If
Next

TextBox1.Lines = lines.ToArray()
  • Related