ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ContainsAny

Returns if a given string contains any of the characters provided in a params array of strings.

Source

public static bool ContainsAny(this string str, params string[] values)
{
    if (!string.IsNullOrEmpty(str) || values.Length == 0)
    {
        foreach (string value in values)
        {
            if(str.Contains(value))
                return true;
        }
    }

    return false;
}

Example

string testString = "Hello world.  This is a test";

bool result = testString.ContainsAny("Hello", "test");

Author: Jason Jackson

Submitted on: 19 nov. 2009

Language: C#

Type: System.String

Views: 4658