如何修复由vb.net中的array.contains方法找到的元素的索引 [英] how to retieve the index of an element which is found by array.contains method in vb.net
本文介绍了如何修复由vb.net中的array.contains方法找到的元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组中大量字符串元素的列表,我使用包含函数来检查它是否包含该元素。它工作正常。现在我想了解元素的索引/位置。假设数组是
dim s as string ()= { first, second, third}
和字符串
dim l as string = third
method
dim b as boolean = s.Contains(l,StringComparer.CurrentCultureIgnoreCase)
flag
if (b)然后
messagebox.show( 存在)
end if
解决方案
使用A. rray.IndexOf
。使用此方法,您甚至不需要包含
:IndexOf
返回-1
如果该项目不存在:
Dim s 作为 字符串()= { first, second, third}
Dim l 作为 字符串 = third
Dim i As Integer = Array.IndexOf(s,l)
如果 i> = 0 然后
控制台.WriteLine( 它存在于索引 + i.ToString())
结束 如果
重要:如果要使用StringComparer.CurrentCultureIgnoreCase
,则不能使用上述内容;在这种情况下你必须使用FindIndex
:
Dim i As Integer = Array.FindIndex(s,函数(x As String )x.Equals(l,StringComparison .CurrentCultureIgnoreCase))
i have a list of large number of string elements in array and i am using contains function to check if it contains that element. it is working fine. Now i want to get to know the index/position of element. suppose the array is
dim s as string() = {"first", "second","third"}
and the string
dim l as string = "third"
method
dim b as boolean = s.Contains(l, StringComparer.CurrentCultureIgnoreCase)
flag
if (b) Then
messagebox.show("It exists")
end if
解决方案
UseArray.IndexOf
. With this method, you don't even needContains
:IndexOf
returns-1
if the item doesn't exist:
Dim s As String() = {"first", "second", "third"} Dim l As String = "third" Dim i As Integer = Array.IndexOf(s, l) If i >= 0 Then Console.WriteLine("it exists at index " + i.ToString()) End If
Important: if you want to useStringComparer.CurrentCultureIgnoreCase
, then you cannot use the above; you'll have to useFindIndex
in that case:
Dim i As Integer = Array.FindIndex(s, Function(x As String) x.Equals(l, StringComparison.CurrentCultureIgnoreCase))
这篇关于如何修复由vb.net中的array.contains方法找到的元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文