加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

.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

您还可以使用高效的HashSet(Of String)而不是不允许重复的列表,如果字符串已经在集合中,则在HashSet.Add中返回False.

Dim isNew As Boolean = lsAuthors.Add(newAuthor)  ' presuming lsAuthors is a HashSet(Of String)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读