[英]How to find multiple values in array?
My code below looks only for one letter, how can I look for combination of letters? 我下面的代码仅查找一个字母,如何查找字母的组合? For ex.: to find letters "ac" in my array and to output them to
textBox2
例如:在我的数组中找到字母“ ac”并将其输出到
textBox2
string[] alphabet = new string[] { "a", "b", "c"};
for (int letter = 0; letter < alphabet.Length; letter++)
{
if (textBox1.Text == alphabet[letter])
textBox2.Text = alphabet[letter];
}
I guess you want to check if only letters of the array are entered in the textbox: 我猜您想检查是否仅在文本框中输入数组的字母:
bool valid = textBox1.Text.All(c => alphabet.Contains(c.ToString()));
if it was a char[]
you could write: 如果是
char[]
,则可以这样写:
bool valid = textBox1.Text.All(alphabet.Contains);
Then you could also use Enumerable.Except
to get the set difference: 然后您还可以使用
Enumerable.Except
来获得设置差异:
var notValidLetters = textBox1.Text.Except(alphabet);
textBox2.Text = "Following are not valid letters: " + String.Join(", ", notValidLetters);
Considering your problem is "Find both a
and c
from the given string ac
"; 考虑到您的问题是“从给定的字符串
ac
同时找到a
和c
”;
string[] alphabet = new string[] { "a", "b", "c"};
for (int letter = 0; letter < alphabet.Length; letter++)
{
if (textBox1.Text.Any(alphabet[letter]))
textBox2.Text += alphabet[letter];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.