Check in the string list

Steven Young 261 Reputation points
2021-03-04T11:16:46.917+00:00

I have the code below, do I have to use the loop? Is there a better way? thank you.

Dim result As Boolean

    Dim a As New List(Of String)
    a.Add("abcd")
    a.Add("qwer")

    Dim x As String = "abcdef"
    For Each s As String In a
        If x.Contains(s) = True Then
            result = True
        End If
    Next
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,622 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-03-04T12:59:13.543+00:00

    Try this alternative:

    Dim a As New List(Of String) From {"abcd", "qwer"}
    Dim x As String = "abcdef"
    
    Dim result As Boolean = a.Any(AddressOf x.Contains)
    

0 additional answers

Sort by: Most helpful