Here's an extension method I wrote for myself:
public static bool IsIn(this string str, string container, bool exact=true)
{
bool result = !string.IsNullOrEmpty(str);
if (result)
{
result = (exact) ? (str == container) || container.IndexOf(str) >= 0
: str.ToUpper() == container.ToUpper() || container.ToUpper().IndexOf(str.ToUpper()) >= 0;
}
return result;
}
Usage would be something like this:
string badStuff = "Text,we,don't,want";
string ourData = "text";
bool thisIsFalse = ourData.IsIn(badStuff);
bool thisIsTrue = ourData.IsIn(badStuff, false);
This idea can be carried into overloads of this method with
IEnumerable<string>
, or even
IEnumerabe<YourObject>, string propertyname
as parameters.
You could even do it with other types:
public static bool IsIn(int this value, string container)
{
return value.ToString().IsIn(container);
}