Vb Net check if arrayList contains a substring

笑着哭i 提交于 2019-12-24 00:43:23

问题


I am using myArrayList.Contains(myString) and myArrayList.IndexOf(myString) to check if arrayList contains provided string and get its index respectively.

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


回答1:


Well you could use the ArrayList to search for substrings with

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.




回答2:


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

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

If you want to test in non case-sensitive manner, you can use String.IndexOf



来源:https://stackoverflow.com/questions/35896721/vb-net-check-if-arraylist-contains-a-substring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!