您可以使用嵌套Any
的StringComparison
重载IndexOf
:
string[] source = { "hi=there", "hello=world", "foo=bar" };
string[] exclude = { "baz", "bar" };
if (!source.Any(src =>
exclude.Any(exl =>
src.IndexOf(exl, StringComparison.InvariantCultureIgnoreCase) >= 0)))
doSomething();
或者打包为扩展方法:
public static class StringExtensions {
public static bool ContainsAny(
this IEnumerable<string> source,
IEnumerable<string> target,
StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase) {
return source.Any(xsource => target.Any(
xtarget => xsource.IndexOf(xtarget, comparisonType) >= 0));
}
}
// Later ...
if (!source.ContainsAny(exclude))
doSomething();