Renato Lulic
Renato Lulic

Reputation: 270

Check if string contains ANY string from the list in Visual Basic

how to check if a string "I want to eat apples", contains a string from the list of strings?

Here's the code that I'm trying to use:

Dim CONTAINER As String() = {"eat", "dog", "cat"}

    If STRING1.Text.Contains(CONTAINER(0..All?)) Then
        Dim ioFileT As New System.IO.StreamReader("C:\strings\RANDOMWORD.txt")
        Dim linesT As New List(Of String)
        Dim rndT As New Random()
        Dim lineT As Integer
        Dim RANDOMWORDFROMTXTFILE As String
        While ioFileT.Peek <> -1
            linesT.Add(ioFileT.ReadLine())
        End While
        lineT = rndT.Next(linesT.Count + 0)
        RANDOMWORDFROMTXTFILE = (lines(line).Trim())
        Console.Write(RANDOMWORDFROMTXTFILE)
    End If

Upvotes: 1

Views: 3080

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54487

If substringList.Any(Function(s) myString.Contains(s)) Then
    'myString contains at least one item from substringList.
End If

Upvotes: 3

Related Questions