-1

This has been asked before in VB (apparently not satisfactory) but not in C#.

I need to add two punctuation marks to my String.Contains method (apart from the already existing "."), namely "!" and "?".

The idea is that the user finishes his/her input with a punctuation mark (i.e period, question mark or exclamation point). If they don't, the input will not be accepted in the text box.

 private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            
            if (textBox1.Text.Contains("."))
                
            {            
               Task.Delay(200).Wait();
                MessageBox.Show("Thanks for your input!");
            }

            else
            {
                return;
            }

How do I add these two? It seems impossible to add them as separate values...

1 Answers1

1

If we need to only check the last character, we can do this using EndsWith method:

private void textBox1_TextChanged_1(object sender, EventArgs e)
{
    var text = textBox1.Text;
    if (text.EndsWith(".") || text.EndsWith("!") || text.EndsWith("?"))
    {            
         Task.Delay(200).Wait();
         MessageBox.Show("Thanks for your input!");
    }
    else
    {
         return;
    }
}

https://learn.microsoft.com/en-us/dotnet/api/system.string.endswith?view=netcore-3.1

ochzhen
  • 156
  • 2
  • 5