-
How to set vb.net Collection object to ignore case and how to detect whether it contains an item
-
In VB.NET, you can use
StringComparer
classes to setCollection
ignore case for objects.StringComparer
The class provides several static properties that can be used to create case-insensitive comparators. whereStringComparer.OrdinalIgnoreCase
the property creates a case-insensitive comparator.Here is a sample code that demonstrates how to set
Collection
ignore case for an object:复制Dim myCollection As New Collection(StringComparer.OrdinalIgnoreCase) myCollection.Add("Apple") myCollection.Add("Banana") myCollection.Add("Orange") ' 检测是否含有某项 If myCollection.Contains("apple") Then Console.WriteLine("包含 'apple'") Else Console.WriteLine("不包含 'apple'") End If
The output will be "contains 'apple'" because
Contains
the method ignores case when comparing.In addition,
Collection
the object also providesContains
methods to detect whether it contains an item. This method accepts a parameter representing the item to detect. Returns ifCollection
the object contains the item,True
otherwiseFalse
.Hope this helps!
-
Unrepeated keys do not require values. What object should be used? You can detect whether it contains a certain KEY from the object.
-
If you only need unique keys and not corresponding values, you can use
HashSet(Of TKey)
objects.HashSet(Of TKey)
Is a collection class that stores unique elements and provides efficient search operations.Here is a sample code that demonstrates how to use
HashSet(Of TKey)
an object to detect whether a key is contained:复制Dim myHashSet As New HashSet(Of String) myHashSet.Add("Apple") myHashSet.Add("Banana") myHashSet.Add("Orange") ' 检测是否含有某个键 If myHashSet.Contains("Apple") Then Console.WriteLine("包含 'Apple'") Else Console.WriteLine("不包含 'Apple'") End If
The output will be "contains 'Apple'" because
Contains
the method can be used to detectHashSet(Of TKey)
whether the object contains a certain key.Hope this helps!
c# vb.net Finds whether a certain key name HashSet, Collection, Dictionary object usage exists
おすすめ
転載: blog.csdn.net/xiaoyao961/article/details/135419553
おすすめ
ランキング