使用C#.net检查字符串是否包含一个数组值

使用C#.net检查字符串是否包含一个数组值,c#,asp.net,.net,arrays,C#,Asp.net,.net,Arrays,我有string[]srt={“t”、“n”、“m”} 我需要知道用户输入是否包含str中的一个值,并打印该值 我尝试了这个代码,但它不适合我 string str=Textbox.Text; 字符串s=“”; 字符串[]a={“m”,“t”,“n”}; if(str.Contains(a.ToString())) { s=s+a; } 其他的 { s=s+“字符串中没有匹配项”; } 标签1.Text=s您需要在数组中搜索str so中的字符串值 if (str.Contains(a.ToS

我有
string[]srt={“t”、“n”、“m”}

我需要知道用户输入是否包含str中的一个值,并打印该值

我尝试了这个代码,但它不适合我

string str=Textbox.Text;
字符串s=“”;
字符串[]a={“m”,“t”,“n”};
if(str.Contains(a.ToString()))
{
s=s+a;
}
其他的
{
s=s+“字符串中没有匹配项”;
}

标签1.Text=s您需要在数组中搜索str so中的字符串值

if (str.Contains(a.ToString()))
会是

if(a.Contains(s))
你的代码是

if (a.Contains(str))
{
    s = s + "," + a;
}
else
{
     s = s + "there is no match in the string";
}

Label1.Text = s;
作为补充说明您应该使用有意义的全名,而不是
a
s

您还可以使用conditional使其更简单

string matchResult = a.Contains(s) ? "found" : "not found"
看 您可以使用Array.IndexOf方法:

string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos >- 1)
{
    // the array contains the string and the pos variable
    // will have its position in the array
}

不需要将数组转换为字符串。如果您不在乎哪个字符匹配,请使用
Any()

如果您想要匹配:

var matches = a.Where(anA => str.Contains(anA));
var s = matches.Any()
    ? "These Match: " + string.Join(",", matches)
    : "There is no match in the string";

小家伙跑不了!!这个问题有点让人困惑,因为似乎用户输入将在
str
中,所以您似乎正在尝试检查用户输入是否包含
a
中的内容,对吗?可能重复我想做的是
var matches = a.Where(anA => str.Contains(anA));
var s = matches.Any()
    ? "These Match: " + string.Join(",", matches)
    : "There is no match in the string";