Check if a string contains an element from a list (of strings)
Asked 07 September, 2021
Viewed 786 times
  • 56
Votes

For the following block of code:

For I = 0 To listOfStrings.Count - 1
    If myString.Contains(lstOfStrings.Item(I)) Then
        Return True
    End If
Next
Return False

The output is:

Case 1:

myString: C:Filesmyfile.doc
listOfString: C:Files, C:Files2
Result: True

Case 2:

myString: C:Files3myfile.doc
listOfString: C:Files, C:Files2
Result: False

The list (listOfStrings) may contain several items (minimum 20) and it has to be checked against a thousands of strings (like myString).

Is there a better (more efficient) way to write this code?

12 Answer