Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3843234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:54:31+00:00 2026-05-19T15:54:31+00:00

I have a List<string> words = new List<string> {word1, word2, word3}; And i want

  • 0

I have a

List<string> words = new List<string> {"word1", "word2", "word3"};

And i want to check using linq if my string contains ANY of these words; Smthng like:

var q = myText.ContainsAny(words);

And the second, if i have a list of sentences too:

List<string> sentences = new List<string> { "sentence1 word1" , "sentence2 word2" , "sentence3 word3"};

and also neet to check if any of these sentence contains any of these words!

 var q = sentences.Where(s=>words.Any(s.text))....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:54:32+00:00Added an answer on May 19, 2026 at 3:54 pm

    You can use a simple LINQ query, if all you need is to check for substrings:

    var q = words.Any(w => myText.Contains(w));
    // returns true if myText == "This password1 is weak";
    

    If you want to check for whole words, you can use a regular expression:

    1. Matching against a regular expression that is the disjunction of all the words:

      // you may need to call ToArray if you're not on .NET 4
      var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b");
      // the following line builds a regex similar to: (word1)|(word2)|(word3)
      var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
      var q = pattern.IsMatch(myText);
      
    2. Splitting the string into words with a regular expression, and testing for membership on the words collection (this will get faster if you use make words into a HashSet instead of a List):

      var pattern = new Regex(@"\W");
      var q = pattern.Split(myText).Any(w => words.Contains(w));
      

    In order to filter a collection of sentences according to this criterion all you have to do is put it into a function and call Where:

     // Given:
     // bool HasThoseWords(string sentence) { blah }
     var q = sentences.Where(HasThoseWords);
    

    Or put it in a lambda:

     var q = sentences.Where(s => Regex.Split(myText, @"\W").Any(w => words.Contains(w)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list: List(Of String) I want to filter it with LINQ. I
I have a list of words (assume they are stored in String[] if you
I have a string which is basically a list of words delimited by commas.
Let's say I have a string Hello and a list words = ['hello', 'Hallo',
I have this bit of code, public static List<string> GetSentencesFromWords(List<string> words, string fileContents) {
I have a list of string values that I want add to a hashtable
I have a list of string like: s = [(abc,bcd,cde),(123,3r4,32f)] Now I want to
I'm using NHunspell to check a string for spelling errors like so: var words
I have a List which contains the list of words that needs to be
Suppose you have output like this: Word1 Word2 Word3 Word4 Where the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.