viv_acious
viv_acious

Reputation: 2489

check string contained in a list of strings

What is the best way to check if a string (of ints) is within a list of string?

E.g. Check whether '1' is in (1,2,9,10,11,15)

I had something like:

  if(listofString.Contains(radiolist.SelectedValue))

where the radiolist.SelectedValue is an integer stored in string form.

I don't think the above would work because the '1' would probably match '11' in the string.

Any ideas?

Thanks!

Upvotes: 0

Views: 210

Answers (2)

Moho
Moho

Reputation: 16498

assuming listOfString = "1,2,9,10,11,15"

if( listOfString.Split( new char[]{','} ).Any( ss => ss == radioList.SelectedValue ) )

Upvotes: 1

Daniel Imms
Daniel Imms

Reputation: 50149

You can split the array by the ',' character and then using .Contains().

string listofString = "1,2,9,10,11,15";
string[] stringInts = listofString.Split(',');

if (stringInts.Contains(radiolist.SelectedValue.ToString()))
{
    // ...
}

Upvotes: 1

Related Questions