2

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

2
  • 1
    Any reason you are using an old ArrayList rather than a List(of String)? Commented Mar 9, 2016 at 16:09
  • yes, because I am currently using some old functions that return an ArrayList. However, no problem to turn into List. Then, same question for Lists. Thanks Commented Mar 9, 2016 at 16:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.