Answered by:
Search for strings in a list that contain specified words

Question
-
Hi everyone
I use extension method Where to filter a list
IEnumerable<string> result = myList<string>.Where(s=>s.Contains(textBox1.Text));
In the myList i have some strings i.e
"a b c d", "c d e f" ,"a b e f", "d e f g"
This works fine but requires me to provide words in specified order in textBox. When i provide string " c d" i get "a b c d" and "c d e f"
but i'd like to get the same result when i provide "d c" and in addition string "d e f g" because 'd' is in it.
What's the best way to do that?
- Edited by PawelM Tuesday, July 3, 2012 7:07 PM
Answers
All replies
-
var result = myList.Where(str => str.Any( character => textBox1.Text.Contains(character) );
If you are going to enter more than just a few characters in the list you should consider loading the textbox text into a HashSet, but for just a few characters it will be just fine as it is.- Proposed as answer by JohnGrove Tuesday, July 3, 2012 7:21 PM
-
I was just fiddling with this. My answer was just a tad more lengthy. Assume the variable "text" is your textBox1.Text
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String[] test = { "abcd", "cdef", "abef", "defg" };
String text = "e";
String[] result = test.Where(s =>
{
Boolean isInThere = true;
Char[] textArray = text.ToCharArray();
foreach (Char c in textArray)
{
if (!s.Contains(c))
isInThere = false;
}
return isInThere;
}).ToArray();
}
}
}
John Grove, Senior Software Engineer http://www.digitizedschematic.com/
-
@John, you could add a break in the 'if' statement of your 'for' loop.
That's one of the advantages of using 'Any', you get the built in short circuiting.
The converting to a char array is also not needed; string implements IEnumerable<char> all on it's own.
-
-
Could you tell me how to use HashSet in this case, because i might need to search longer strings?
What if the List contains long strings (sentences) and i want to find all sentences that contain words written in TextBox?
ie
List<string> sentences = new List<string>(); sentences.Add("This is a long sentence."); sentences.Add("This is another long sentence."); sentences.Add(... //adding more sentences
How to search that list(the words in the TextBox are in custom order)?
- Edited by PawelM Thursday, July 5, 2012 5:42 AM
-
var searchWords = new HashSet<string>(textbox1.Text.Split(' ')); var results = sentances.Where(sentance => sentance.Split(' ') .Any(word => searchWords.Contains(word)));
Note that what you're doing won't scale particularly well, so if you have a lot of strings that you're searching this could get fairly slow. -
-
-
-
Find the items of myList for which there is at least 1 word among those from the textbox:
IEnumerable<string> result = myList.Where(s => s.Split().Intersect(textBox1.Text.Split()).Any());
-
-
-
-
-
My previous sample does the same thing as this one:
string[] wordsToFind = textBox1Text.Split(); IEnumerable<string> result = myList.Where(s => wordsToFind.All(w => s.Split().Contains(w)));
Every time you want a delegate and write a lambda of the form x => foo(x) , you could simply use the method foo directly.