0

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

string[] alphabet = new string[] { "a", "b", "c"};
for (int letter = 0; letter < alphabet.Length; letter++)
{
    if (textBox1.Text == alphabet[letter])
    textBox2.Text = alphabet[letter];
}
4
  • Do you want to check if all symbols in textBox1.Text are in the alphabet? Commented May 12, 2015 at 9:49
  • 1
    It's not clear, how is ""ac"" related to your array? Commented May 12, 2015 at 9:49
  • @Andrei yes, and output them to textbox2; Commented May 12, 2015 at 9:51
  • @LittleFox: what do you want to output? If all are in the string[], do you want to ouput the complete string[]? If so, what is the desired result? Mabye: string.Join(",", alphabet ) Commented May 12, 2015 at 9:54

2 Answers 2

3

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:

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

Then you could also use Enumerable.Except to get the set difference:

var notValidLetters = textBox1.Text.Except(alphabet);
textBox2.Text = "Following are not valid letters: " + String.Join(", ", notValidLetters);
Sign up to request clarification or add additional context in comments.

Comments

0

Considering your problem is "Find both a and c from the given string 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];
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.