gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:23:36 25 4
gpt4 key购买 nike

我有 string[] srt={"t","n","m"}

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

我试过这段代码,但它对我不起作用

string str = Textbox.Text;
string s = "";
string[] a = {"m","t","n"};

if (str.Contains(a.ToString()))
{
s = s + a;
}

else
{
s = s + "there is no match in the string";
}

Label1.Text = s;

最佳答案

您需要在数组中搜索 str 中的字符串值

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;

作为补充说明,您应该使用含义全名而不是as

您还可以使用条件 operator ?:使其更简单。

string matchResult = a.Contains(s) ? "found" : "not found"

关于c# - 使用 C# .net 检查字符串是否包含数组值之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com