Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if there is any of the chars inside the textbox

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");
    }
}
like image 436
Ali Çarıkçıoğlu Avatar asked Jan 13 '23 22:01

Ali Çarıkçıoğlu


1 Answers

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;
}
like image 119
p.s.w.g Avatar answered Jan 30 '23 12:01

p.s.w.g