If you want to avoid using Linq for now then this works
Dim tbLines = TextBox1.Text.Split(Environment.NewLine).ToList()
Dim tblNew = New List(Of String)
Dim sb = New StringBuilder("")
For Each s As String In tbLines
If Not tblNew.Contains(s.Trim()) Then
tblNew.Add(s.Trim())
sb.Append(s.Trim())
sb.Append(Environment.NewLine)
End If
Next
If sb.Length > Environment.NewLine.Length Then
sb.Length -= Environment.NewLine.Length
End If
TextBox2.Text = sb.ToString()
Or ... a bit neater and using Linq
Dim tbLines = TextBox1.Text.Split(Environment.NewLine).ToList()
Dim tblNew = New List(Of String)
Dim sb = New StringBuilder("")
For Each s As String In From s1 In tbLines Where Not tblNew.Contains(s1.Trim())
tblNew.Add(s.Trim())
sb.Append(s.Trim())
sb.Append(Environment.NewLine)
Next
If sb.Length > Environment.NewLine.Length Then
sb.Length -= Environment.NewLine.Length
End If
TextBox2.Text = sb.ToString()
There is no need for two loops - I've used
List(Of String)
because I prefer lists to arrays and used the built in
.Contains
. If you want to keep it as an array then arrays also have a
.Contains
method.
I'm also using a
StringBuilder
to build up the new text rather than adding it directly to the new TextBox text. This is because strings are
immutable, meaning they cannot be changed. Everytime you use
TextBox1.Text += ReadlineValue + Environment.NewLine
you are actually getting a brand new string.
The only other thing to note is that I've trimmed the strings before adding them - this is because you have both "one" and " one" in your test data.