.net – 检查字符串列表是否包含值
发布时间:2020-12-17 00:05:13 所属栏目:大数据 来源:网络整理
导读:我有: Public lsAuthors As List(Of String) 我想在此列表中添加值,但在添加之前我需要检查确切的值是否已经在其中.我怎么知道这个? 你可以使用 List.Contains : If Not lsAuthors.Contains(newAuthor) Then lsAuthors.Add(newAuthor)End If 或者使用LINQ
我有:
Public lsAuthors As List(Of String) 我想在此列表中添加值,但在添加之前我需要检查确切的值是否已经在其中.我怎么知道这个?
你可以使用
List.Contains :
If Not lsAuthors.Contains(newAuthor) Then lsAuthors.Add(newAuthor) End If 或者使用LINQs Enumerable.Any: Dim authors = From author In lsAuthors Where author = newAuthor If Not authors.Any() Then lsAuthors.Add(newAuthor) End If 您还可以使用高效的 Dim isNew As Boolean = lsAuthors.Add(newAuthor) ' presuming lsAuthors is a HashSet(Of String) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |