You can Split
your string on white space and then use Enumerable.Intersect
like:
var matching = colors.Intersect(myString.Split());
The above would return one item in matching
, i.e. red
If you want case-insensitive then you can do:
var matching = colors.Intersect(myString.Split(),
StringComparer.InvariantCultureIgnoreCase);
EDIT: If you are looking for Partial matching or multiple words matching then you can do:
List<string> colors = new List<string> { "red", "red apple", "yellow", "app" };
string myString = "There is a red apple";
var partialAllMatched =
colors
.Where(r => myString.IndexOf(r, StringComparison.InvariantCultureIgnoreCase) >=0);
This would return you:
red
red apple
app