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

时间:2015-05-12 09:47:58

标签: c# arrays

下面的代码只显示一个字母,我该如何查找字母组合?例如:在我的数组中查找字母“ac”并将其输出到[HttpGet] public ActionResult Index(string message) { var Result = from Client in db.Clients where Clients.ClientName == message select Clients.ClientName; ViewBag.Message = Result.ToList(); getCurrentUser(); return View(); }

textBox2

2 个答案:

答案 0 :(得分:3)

我想您要检查是否只在文本框中输入了数组的字母:

bool valid = textBox1.Text.All(c => alphabet.Contains(c.ToString()));

如果它是char[]你可以写:

bool valid = textBox1.Text.All(alphabet.Contains);

然后您还可以使用Enumerable.Except来获得设置差异:

var notValidLetters = textBox1.Text.Except(alphabet);
textBox2.Text = "Following are not valid letters: " + String.Join(", ", notValidLetters);

答案 1 :(得分:0)

考虑到您的问题是"从给定字符串a"&#34获取cac;

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];
        }