简体   繁体   中英

How to check if any word in my List<string> contains in text

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))....

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)));
var q = words.Any(w => myText.Contains(w));

To return all the sentences that contain 1 or more of the words:

var t = sentences.Where(s => words.Any(w => s.Contains(w)));

            foreach (var sentence in t)
            {
                Console.WriteLine(sentence);
            }

For your first condition

List<string> words = new List<string> { "word1", "word2", "word3" };
string test = "word1";
bool isFound = words.Contains(test);

For your second condition

bool isFound = sentences.Any(x => x.Split(new char[] { ' ' }).Contains(test));

As an unrelated side-note

You are changing the question's scope after getting some answers and that is not a good way to ask questions. :)

While most of the solutions presented are functional (the all call Contains , which will give you the desired solution), if the list and the text are large, you may have performance issues.

From the problem that was stated, I'd think that you call a word anything between spaces or any other divisor. So, I'd recommend you split your myText into a list of words, and compare each one of them to you list of words, now using contains.

It's way more complex, of course; You'd have to make sure split words correctly -- but with larger strings (for example, a text file) there might be some performance gain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM