I have a chararray on global, button and textbox, how do I check if the word in textBox1.Text contains the letters in the chararray?
char[] letters = { 'a', 'e' };
private void button1_Click(object sender, EventArgs e)
{
bool containsAnyLetter = textBox1.Text.IndexOfAny(letters) >= 0;
if (containsAnyLetter == true)
{
MessageBox.Show("your word contains a or e");
}
}
You can do this to see if the string contains any of the letters:
private void button1_Click(object sender, EventArgs e)
{
bool containsAnyLetter = letters.Any(c => textBox1.Text.Contains(c));
}
Or more simply:
private void button1_Click(object sender, EventArgs e)
{
bool containsAnyLetter = textBox1.Text.IndexOfAny(letters) >= 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With