Uppercase and lowercase regular expression

I am creating a simple C # application in which it has a condition for writing both uppercase and lowercase letters.

This is my condition:

if( txtChord.Text == "A" || txtChord.Text == "a" && cbKeys.SelectedIndex == 6 ){ txtAnswer.Text = "B"; } 

I would like to do this more efficiently with regex.

+4
source share
5 answers

Use the String.Compare(String, String, Boolean) method String.Compare(String, String, Boolean) and set true for the last argument to ignore case.

The method above returns a negative, 0, or positive number.

If you want only a bool value, you can use String.Equals(String, String, StringComparison) with the StringComparison.OrdinalIgnoreCase option.

+7
source

What is also often done to ignore looks something like this:

 if( txtChord.Text.ToLower() == "a" && cbKeys.SelectedIndex == 6 ) 

But note that in your if two checks are not โ€œequivalentโ€ because && has a higher precendance than || . Your equivalent:

 if( txtChord.Text == "A" || (txtChord.Text == "a" && cbKeys.SelectedIndex == 6)) 

Unable to replace one check.

+4
source

You do not need regex for this. You can simply:

 if(txtChord.Text.ToLower() == "a" && cbKeys.SelectedIndex ==6) { txtAnswer.Text = "B"; } 

ToLower() will make any text in the txtChord text box lowercase, and then you can check it. Or you can use ToUpper() . This is the same, but with capital letters.

+1
source

Regex.Match (txtChord.Text, "a", RegexOptions.IgnoreCase)

+1
source

There is a ton of good information here: http://www.regular-expressions.info/

To catch an uppercase or lowercase letter, you can use "[Aa]" or, possibly, also with a flag.

0
source

All Articles