如何在多行文本框中删除重复的字符串 [英] How do I remove duplicate string in a multi-line text box
问题描述
Public Class Form1
Dim readlinevalue As String = ""
Dim readlinevalue2 As String = ""
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 1
For Each ReadlineValue As String In Me.TextBox1.Text.Split(Environment.NewLine)
Console.WriteLine("firstloop")
For Each ReadlineValue2 As String In Me.TextBox1.Text.Split(Environment.NewLine)
Console.WriteLine("secondloop")
Do Until i = 100
Console.WriteLine("Do_loop 100")
TextBox1.Lines = Array.FindAll(TextBox1.Lines, Function(line) Not line.Contains(ReadlineValue))
i = i + 1
Loop
i = 1
Next
TextBox1.Text += ReadlineValue + Environment.NewLine
If readlinevalue2.Contains(ReadlineValue) = False Then
Console.WriteLine("true")
TextBox2.Text += ReadlineValue + Environment.NewLine
End If
Next
End Sub
End Class
< br $> b $ b
我尝试了什么:
i已经尝试了所有的东西
测试这一个
-------------------------------- --------
一个
两个
三个
一个
两个
三个
一个
两个
三个
--- -------------------------------
What I have tried:
i have tried all most every thing
test this one
----------------------------------------
one
two
three
one
two
three
one
two
three
----------------------------------
推荐答案
如果你我现在想避免使用Linq然后这个工作
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
'Remove the trailing NewLine
If sb.Length > Environment.NewLine.Length Then
sb.Length -= Environment.NewLine.Length
End If
TextBox2.Text = sb.ToString()
或者......有点整洁使用Linq
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
'Remove the trailine NewLine
If sb.Length > Environment.NewLine.Length Then
sb.Length -= Environment.NewLine.Length
End If
TextBox2.Text = sb.ToString()
不需要两个循环 - 我使用 List(Of String)
因为我更喜欢列表到数组并使用内置的 .Contains
。如果你想把它保存为一个数组,那么数组也有一个 .Contains
方法。
我是还使用 StringBuilder
来构建新文本,而不是直接将其添加到新的TextBox文本中。这是因为字符串不可变,这意味着它们无法更改。每次你使用
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
你实际上得到一个全新的字符串。
唯一需要注意的是我在添加字符串之前修剪了字符串 - 这是因为你的测试数据中同时包含one和one。
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.
这篇关于如何在多行文本框中删除重复的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!