简体   繁体   中英

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?

Eg 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!

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

假设listOfString =“1,2,9,10,11,15”

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM