简体   繁体   English

如何在数组中查找多个值?

[英]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同时找到ac ”;

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.

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