[RESOLVED] Deleting repeated Lines in Textbox
Hi all.
If I have a multi-line textbox like this:
----
Line 1
Line 1
Line 1
Line 2
Line 2
Line 3
----
How do I remove lines that are the same as the line above?
This is my code:
vb.net Code:
For i = 0 To txtTextResult.Lines.Count - 1
Dim sLine As String
sLine = txtTextResult.Lines(i)
Try
Dim sNextLine As String = txtTextResult.Lines(i + 1)
If sNextLine = sLine Then
'Remove txtTextResult.Lines(i + 1)
End If
Catch : Exit For : End Try 'When sLine is the last line, sNextLine can't be the line under it.
Next
I've already tried many things.
For example, this, which is not working:
vb.net Code:
txtTextResult.Lines(i + 1).Remove(0) 'Remove ( startindex as integer)
Thanks.
Re: Deleting repeated Lines in Textbox
try this:
vb Code:
Dim lines As New List(Of String)(txtTextResult.Lines)
For x As Integer = lines.Count - 1 To 0 Step -1
Dim currentLine As String = lines(x)
Dim matchCount = Aggregate line As String In lines _
Where line = currentLine Into Count()
For m As Integer = 1 To matchCount - 1
lines.Remove(currentLine)
Next
Next
txtTextResult.Lines = lines.ToArray
Re: Deleting repeated Lines in Textbox
I don't think .paul.'s solution will work because it would remove duplicates even if they weren't adjacent. Try this:
vb.net Code:
Dim lines As New List(Of String)(txtTextResult.Lines)
For index As Integer = lines.Count - 1 To 1 Step -1
If lines(index) = lines(index - 1) Then
lines.RemoveAt(index)
End If
Next
txtTextResult.Lines = lines.ToArray()
Note that the code reads pretty much exactly as you described the requirement: if a line is the same as the line above, remove that line. Note, also that the loop goes from the end to the beginning, allowing you to remove items without affecting the indexes of those items yet to be checked. Finally, note that the last index used is 1 rather than 0, because you can't compare the first line with the line above it.
Re: Deleting repeated Lines in Textbox
Thank you for the reply,
it works well and I was stuck at this easy piece of code.
Re: Deleting repeated Lines in Textbox
Quote:
Originally Posted by
Cooi
Thank you for the reply,
it works well and I was stuck at this easy piece of code.
The secret is looping backwards. Whenever you want to remove items from a collection in a loop you pretty much always have to loop backwards.
Re: [RESOLVED] Deleting repeated Lines in Textbox
Hi, i'm new here.
The problem which i need is how to remove dublicates from this code ?
I have tried many times to find how to remove dublicates in textbox but no luck ;(
So thought maybe you can help me because i can't find an answers to this question.
Here is my code.
Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("href").Contains("xvideos") Then
TextBox2.Text += curElement.GetAttribute("innerText") & vbCrLf
End If
Next
End Sub
Re: [RESOLVED] Deleting repeated Lines in Textbox
Quote:
Originally Posted by
polas
Hi, i'm new here.
The problem which i need is how to remove dublicates from this code ?
I have tried many times to find how to remove dublicates in textbox but no luck ;(
So thought maybe you can help me because i can't find an answers to this question.
Here is my code.
Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("href").Contains("xvideos") Then
TextBox2.Text += curElement.GetAttribute("innerText") & vbCrLf
End If
Next
End Sub
you should start your own thread for this question