How to check if a String contains any of some strings

Well, there’s always this:

public static bool ContainsAny(this string haystack, params string[] needles)
{
    foreach (string needle in needles)
    {
        if (haystack.Contains(needle))
            return true;
    }

    return false;
}

Usage:

bool anyLuck = s.ContainsAny("a", "b", "c");

Nothing’s going to match the performance of your chain of || comparisons, however.

Leave a Comment