Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look for words in strings with LINQ

Tags:

c#

vb.net

linq

C# or VB.NET suggestion are welcome.

I have the following code:

 Dim someText = "Stack Over Flow Community"
    Dim someWord = "Over Community"

    If someText.Contains(someWord) Then
        Response.Write("Word found.")
    Else
        Response.Write("No word found.")
    End If


 Function Contains looks only for next words from left to right. 

someText.Contains("Over Stack")  returns False
someText.Contains("Stack Community")  returns False

I want all of these to return True as long as there are words that exist in the string.

Is there any existing function that cover any case regardless of words position in the string?

like image 827
Narazana Avatar asked Dec 13 '22 13:12

Narazana


2 Answers

words.Split(' ').Any(someText.Contains)
like image 135
Snowbear Avatar answered Dec 15 '22 04:12

Snowbear


someText.Split(' ').Intersect(someWord.Split(' ')).Any();

like image 23
George Duckett Avatar answered Dec 15 '22 04:12

George Duckett