Vb Net 检查 arrayList 是否包含子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35896721/
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
Vb Net check if arrayList contains a substring
提问by Jaume
I am using myArrayList.Contains(myString)and myArrayList.IndexOf(myString)to check if arrayList contains provided string and get its index respectively.
我正在使用myArrayList.Contains(myString)和myArrayList.IndexOf(myString)检查 arrayList 是否包含提供的字符串并分别获取其索引。
But, How could I check if contains a substring?
但是,如何检查是否包含子字符串?
Dim myArrayList as New ArrayList()
myArrayList.add("sub1;sub2")
myArrayList.add("sub3;sub4")
so, something like, myArrayList.Contains("sub3")should return True
所以,像这样,myArrayList.Contains("sub3")应该返回 True
采纳答案by Steve
Well you could use the ArrayList to search for substrings with
那么你可以使用 ArrayList 来搜索子字符串
Dim result = myArrayList.ToArray().Any(Function(x) x.ToString().Contains("sub3"))
Of course the advice to use a strongly typed List(Of String) is absolutely correct.
当然,使用强类型 List(Of String) 的建议是绝对正确的。
回答by T.S.
As far as your question goes, without discussing why do you need ArrayList, because array list is there only for backwards compatibility - to select indexes of items that contain specific string, the best performance you will get here
就您的问题而言,无需讨论为什么需要 ArrayList,因为数组列表仅用于向后兼容 - 选择包含特定字符串的项目的索引,您将在此处获得最佳性能
Dim indexes As New List(Of Integer)(100)
For i As Integer = 0 to myArrayList.Count - 1
If DirectCast(myArrayList(i), String).Contains("sub3") Then
indexes.Add(i)
End If
Next
Again, this is if you need to get your indexes. In your case, ArrayList.Contains- you testing whole object [string in your case]. While you need to get the string and test it's part using String.Contains
同样,这是如果您需要获取索引。在你的情况下,ArrayList.Contains- 你测试整个对象 [string in your case]。虽然您需要获取字符串并测试它的一部分使用String.Contains
If you want to test in non case-sensitive manner, you can use String.IndexOf
如果要以不区分大小写的方式进行测试,可以使用String.IndexOf

