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

时间:2021-06-21 01:35:16

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)

例如。检查'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!

谢谢!

2 个解决方案

#1


1  

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

#2


1  

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

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

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

#1


1  

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

#2


1  

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

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

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