Reputation: 2489
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
Reputation: 16498
assuming listOfString = "1,2,9,10,11,15"
if( listOfString.Split( new char[]{','} ).Any( ss => ss == radioList.SelectedValue ) )
Upvotes: 1
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