简体   繁体   English

检查字符串列表中包含的字符串

[英]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) 例如,检查'1'是否在(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. 其中radiolist.SelectedValue是以字符串形式存储的整数。

I don't think the above would work because the '1' would probably match '11' in the string. 我不认为上述内容会起作用,因为'1'可能与字符串中的'11'匹配。

Any ideas? 有任何想法吗?

Thanks! 谢谢!

You can split the array by the ',' character and then using .Contains() . 您可以使用','字符拆分数组,然后使用.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 ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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