Vb.net 从列表中删除重复项
我有以下课程:Vb.net 从列表中删除重复项,vb.net,Vb.net,我有以下课程: Public Class titlesclass Public Property Link As String Public Property Title As String Public Function Clear() Link.Distinct().ToArray() Title.Distinct().ToArray() End Function End Class 以及以下代码: For Each title As Match In (New Reg
Public Class titlesclass
Public Property Link As String
Public Property Title As String
Public Function Clear()
Link.Distinct().ToArray()
Title.Distinct().ToArray()
End Function
End Class
以及以下代码:
For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
Dim letitre As New titlesclass
letitre.Link = title.Groups("Data").Value
letitre.Title = title.Groups("Dataa").Value
lestitres.Add(letitre)
'tempTitles2.Add(title.Groups("Dataa").Value)
Next
我尝试用简单的方法删除重复的字符串
Dim titles2 = lestitres.Distinct().ToArray()
并调用类函数:
lestitres.Clear()
但是这两个命题都不起作用,我知道我缺少了一些非常简单的东西,但仍然无法找到使用已经实现的类更容易的方法: 或:
更易于使用已实现以下功能的类: 或:
好的,因为我注意到您使用的是
类
这里有一个选项,您可以这样做,以避免在类中向
列表
添加重复项。我使用控制台应用程序编写此示例,如果需要,理解并转换为Windows窗体应用程序应该不会太难
Module Module1
Sub Main()
Dim titlesClass = New Titles_Class()
titlesClass.addNewTitle("myTitle") ''adds successfully
titlesClass.addNewTitle("myTitle") '' doesn't add
End Sub
Public Class Titles_Class
Private Property Title() As String
Private Property TitleArray() As List(Of String)
Public Sub New()
TitleArray = New List(Of String)()
End Sub
Public Sub addNewTitle(title As String)
Dim added = False
If Not taken(title) Then
Me.TitleArray.Add(title)
added = True
End If
Console.WriteLine(String.Format("{0}", If(added, $"{title} has been added", $"{title} already exists")))
End Sub
Private Function taken(item As String) As Boolean
Dim foundItem As Boolean = False
If Not String.IsNullOrEmpty(item) Then
foundItem = Me.TitleArray.Any(Function(c) -1 < c.IndexOf(item))
End If
Return foundItem
End Function
End Class
End Module
抛开这些不谈,您是否考虑过使用一个,这样您就可以将标题作为键,将链接作为值,这将是另一种方式,使列表(字典)不包含重复的项目好的,由于我注意到您使用的是
类
这里有一个选项,您可以这样做,以避免在类中向
列表
添加重复项。我使用控制台应用程序编写此示例,如果需要,理解并转换为Windows窗体应用程序应该不会太难
Module Module1
Sub Main()
Dim titlesClass = New Titles_Class()
titlesClass.addNewTitle("myTitle") ''adds successfully
titlesClass.addNewTitle("myTitle") '' doesn't add
End Sub
Public Class Titles_Class
Private Property Title() As String
Private Property TitleArray() As List(Of String)
Public Sub New()
TitleArray = New List(Of String)()
End Sub
Public Sub addNewTitle(title As String)
Dim added = False
If Not taken(title) Then
Me.TitleArray.Add(title)
added = True
End If
Console.WriteLine(String.Format("{0}", If(added, $"{title} has been added", $"{title} already exists")))
End Sub
Private Function taken(item As String) As Boolean
Dim foundItem As Boolean = False
If Not String.IsNullOrEmpty(item) Then
foundItem = Me.TitleArray.Any(Function(c) -1 < c.IndexOf(item))
End If
Return foundItem
End Function
End Class
End Module
除此之外,您是否考虑过使用一个,这样您就可以将标题作为键,将链接作为值,这将是另一种方式,使列表(字典)不包含重复项我有个坏消息。。您并没有遗漏一些简单的东西,String.Distinct().ToArray()并没有做您认为它可以做的事情,类必须实现IComparable for Distinct才能知道如何比较它们,看看您是否可以利用它们。我有个坏消息。。您并没有遗漏一些简单的东西,String.Distinct().ToArray()并没有做您认为它可以做的事情,类必须实现IComparable for Distinct才能知道如何比较它们,看看您是否可以利用它们。
Module Module1
Sub Main()
Dim titlesClass = New Titles_Class()
titlesClass.addNewTitle("myTitle") ''adds successfully
titlesClass.addNewTitle("myTitle") '' doesn't add
End Sub
Public Class Titles_Class
Private Property Title() As String
Private Property TitleArray() As List(Of String)
Public Sub New()
TitleArray = New List(Of String)()
End Sub
Public Sub addNewTitle(title As String)
Dim added = False
If Not taken(title) Then
Me.TitleArray.Add(title)
added = True
End If
Console.WriteLine(String.Format("{0}", If(added, $"{title} has been added", $"{title} already exists")))
End Sub
Private Function taken(item As String) As Boolean
Dim foundItem As Boolean = False
If Not String.IsNullOrEmpty(item) Then
foundItem = Me.TitleArray.Any(Function(c) -1 < c.IndexOf(item))
End If
Return foundItem
End Function
End Class
End Module
Sub Main()
Dim titlesClass = New HashSet(Of String)
titlesClass.Add("myTitle") ''adds successfully
titlesClass.Add("myTitle") '' doesn't add
For Each title As String In titlesClass
Console.WriteLine(title)
Next
End Sub