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];
}
2 个解决方案
#1
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);
#2
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];
}
#1
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);
#2
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];
}