471,318 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

C# has InList() function?

ABC
Is there any function in C# to do as:

result true if InList('abc,ihjl,dkfd,ooo', 'abc');

Jan 11 '06 #1
5 14759
ABC wrote:
Is there any function in C# to do as:

result true if InList('abc,ihjl,dkfd,ooo', 'abc');


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)
{
...
}

Jon

Jan 11 '06 #2
ABC
Wow,

this method is best. In fact, I don't know it has IList class.

Very Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
ABC wrote:
Is there any function in C# to do as:

result true if InList('abc,ihjl,dkfd,ooo', 'abc');


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)
{
...
}

Jon

Jan 11 '06 #3
ABC,

I just want to add that the you even don't need to declare a string variable
you can call the methods directly on the string constant like

bool f = "sdasdadasd".IndexOf("dad") != -1;

*f* will be *true* of the string contains the substring.
--

Stoitcho Goutsev (100)

"ABC" <ab*@abc.com> wrote in message
news:eu**************@tk2msftngp13.phx.gbl...
Wow,

this method is best. In fact, I don't know it has IList class.

Very Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
ABC wrote:
Is there any function in C# to do as:

result true if InList('abc,ihjl,dkfd,ooo', 'abc');


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)
{
...
}

Jon


Jan 11 '06 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
ABC wrote:
Is there any function in C# to do as:

result true if InList('abc,ihjl,dkfd,ooo', 'abc');
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)
{
...
}


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?

Jon

Jan 11 '06 #5
Tim Bird wrote:
string x = "abc,ihjl,dkfd,ooo";
if (x.StartsWith("abc") || x.IndexOf(",abc") != -1)
{
...
}


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?


No, you're absolutely right. You potentially want:

if (x.StartsWith("abc,") ||
x.IndexOf(",abc,") != -1 ||
x.EndsWith(",abc") ||
x=="abc")

String.Split would still probably be less efficient, but would be
simpler at that stage :)

Good catch.

Jon

Jan 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
5 posts views Thread by phil_gg04 | last post: by
2 posts views Thread by laredotornado | last post: by
2 posts views Thread by sushil | last post: by
8 posts views Thread by Olov Johansson | last post: by
3 posts views Thread by Beta What | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.