You could use the Any function if you use the LINQ Method extensions (.net 3.5).
like this:
var foobar = "foobar";
new[] {"foo", "bar"}.Any(foobar.Contains);
but if you dissasemble Any you'll see it's just a loop anyway.
public static bo
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
foreach (TSource local in source)
{
if (predicate(local))
{
return true;
}
}
return false;
}
So if you don't want to use .net 3.5 the above function in .Net 2.0 would look like this:
public static class Utilities
{
public delegate bool AnyPredicate<T>(T arg);
public static bool Any<TSource>(IEnumerable<TSource> source, AnyPredicate<TSource> predicate)
{
if (source == null)
{
throw new ArgumentException("source");
}
if (predicate == null)
{
throw new ArgumentException("predicate");
}
foreach (TSource local in source)
{
if (predicate(local))
{
return true;
}
}
return false;
}
}
usage:
var foobarlist = new[]{"foo", "bar"};
var foobar = "foobar";
var containsfoobar = Utilities.Any(foobarlist, foobar.Contains);
I hope that helps.