A
ABC
Is there any function in C# to do as:
result true if InList('abc,ihjl,dkfd,ooo', 'abc');
result true if InList('abc,ihjl,dkfd,ooo', 'abc');
ABC said:Is there any function in C# to do as:
result true if InList('abc,ihjl,dkfd,ooo', 'abc');
Jon Skeet said:Are you specifically wanting it for that usage? IList has the Contains
method, and Array (which implements IList explicitly) also has IndexOf.
So, you're fine if you've actually already got a list. You can get a
list from a string using string.Split, but for the example you gave it
might be simpler to use:
string x = "abc,ihjl,dkfd,ooo";
if (x.StartsWith("abc") || x.IndexOf(",abc") != -1)
{
...
}
Tim said:Will that fail if x = 'abcd,xxx,yyy,zzz' or x = 'xxx,abcd,yyy', etc? Or am I
just too tired to think of this properly at the moment?
public static bool IsIn<T>(this T source, params T[] values)
{
return values.Contains(source);
}
string myStr = "str3";
bool found = myStr.IsIn("str1", "str2", "str3", "str4");