How to check if a string contains any element of a List<string>?

N K picture N K · Sep 6, 2012 · Viewed 10.2k times · Source

I have an if statement, where I would like to check, if a string contains any item of a list<string>.

if (str.Contains(list2.Any()) && str.Contains(ddl_language.SelectedValue))
{
    lstpdfList.Items.Add(str);
}

Answer

Jon picture Jon · Sep 6, 2012

The correct formulation is

list2.Any(s => str.Contains(s))

This is read as "does list2 include any string s such that str contains s?".