.net 检查字符串列表是否包含值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26732563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:50:48  来源:igfitidea点击:

Check if a list of strings contains a value

.netvb.netlist

提问by Medise

I have:

我有:

Public lsAuthors As List(Of String)

I want to add values to this list, but before adding I need to check if the exact value is already in it. How do I figure that out?

我想向这个列表添加值,但在添加之前我需要检查确切的值是否已经在其中。我如何弄清楚?

回答by Tim Schmelter

You can use List.Contains:

您可以使用List.Contains

If Not lsAuthors.Contains(newAuthor) Then
    lsAuthors.Add(newAuthor)
End If

or with LINQs Enumerable.Any:

或使用 LINQ Enumerable.Any

Dim authors = From author In lsAuthors Where author = newAuthor
If Not authors.Any() Then
    lsAuthors.Add(newAuthor)
End If

You could also use an efficient HashSet(Of String)instead of the list which doesn't allow duplicates and returns Falsein HashSet.Addif the string was already in the set.

你也可以使用一个有效的HashSet(Of String),而不是列表,它不允许重复,并返回FalseHashSet.Add字符串是否已经在集合。

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

回答by Steve

The generic List has a method called Containsthat returns true if the default comparer for the choosen type finds an element matching the searching criteria.

泛型 List 有一个名为Contains的方法,如果所选类型的默认比较器找到与搜索条件匹配的元素,则该方法返回 true。

For a List(Of String) this is the normal string comparison, so your code could be

对于 List(Of String) 这是正常的字符串比较,因此您的代码可能是

Dim newAuthor = "Edgar Allan Poe"
if Not lsAuthors.Contains(newAuthor) Then
    lsAuthors.Add(newAuthor)
End If 

As a side note, the default comparison for strings considers two strings different if they don't have the same case. So if your try to add an author named "edgar allan poe" and you already have added one with the name "Edgar Allan Poe" the barebone Contains fails to notice that they are the same.
If you have to manage this situation then you need

附带说明一下,如果两个字符串的大小写不同,则字符串的默认比较会认为它们是不同的。因此,如果您尝试添加名为“edgar allan poe”的作者,并且您已经添加了名为“Edgar Allan Poe”的作者,那么准系统包含无法注意到它们是相同的。
如果您必须处理这种情况,那么您需要

....
if Not lsAuthors.Contains(newAuthor, StringComparer.CurrentCultureIgnoreCase) Then
    .....

回答by Steve

To check whether an element is present in a list you can use the list.Contains()method. If you are using a button click to populate the list of strings then see the code:

要检查元素是否存在于列表中,您可以使用list.Contains()方法。如果您使用按钮单击来填充字符串列表,请查看代码:

Public lsAuthors As List(Of String) = New List(Of String) ' Declaration of an empty list of strings

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' A button click populates the list
    If Not lsAuthors.Contains(TextBox2.Text) Then ' Check whether the list contains the item that to be inserted
        lsAuthors.Add(TextBox2.Text) ' If not then add the item to the list
    Else
        MsgBox("The item Already exist in the list") ' Else alert the user that item already exist
    End If
End Sub

Note: Line by line explanation is given as comments

注意:逐行解释作为注释给出

回答by Hyman Gajanan

You can get a list of matching items of your condition like this:

您可以获得符合您条件的匹配项目列表,如下所示:

Dim lsAuthors As List(Of String)

Dim ResultData As String = lsAuthors.FirstOrDefault(Function(name) name.ToUpper().Contains(SearchFor.ToUpper()))
If ResultData <> String.Empty Then
    ' Item found
Else
    ' Item Not found
End If