C#: Extension method to compare string against multiple other specific words

Standard
public static class Utilities
{
     public static bool ContainsAny(this string data, params string[] filter)
     {
            foreach (string term in filter)
            {
                var item = term.Trim();
                if (!string.IsNullOrEmpty(item))
                {
                    if (data.ToLower().Contains(term))
                        return true;
                }
            }
            return false;
     }
}

Usage:

string temp = "Hello World! Hello World! Hello World! Hello World!";
string[] filter = {"abc", "xyz", "hello"};
bool result = temp.ContainsAny(filter);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s