|
-
Apr 7th, 2010, 06:05 PM
#1
Thread Starter
Member
[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.
-
Apr 7th, 2010, 06:42 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 8th, 2010, 12:25 AM
#3
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.
Last edited by jmcilhinney; Apr 8th, 2010 at 12:29 AM.
-
Apr 8th, 2010, 02:50 AM
#4
Thread Starter
Member
Re: Deleting repeated Lines in Textbox
Thank you for the reply,
it works well and I was stuck at this easy piece of code.
-
Apr 8th, 2010, 02:52 AM
#5
Re: Deleting repeated Lines in Textbox
 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.
-
Jun 27th, 2011, 04:21 AM
#6
Addicted Member
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
-
Jun 27th, 2011, 06:23 AM
#7
Re: [RESOLVED] Deleting repeated Lines in Textbox
 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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|