Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# If Equals Case insensitive [duplicate]

The following code will open up a Message Box containing the word "Fail".

Is there a way to make the if statement case insensitive, so that the if statement passes and opens a mbox containg "Pass" without converting the character/string to upper/lower case?

here is the code:

public partial class Form1 : Form
    {
        string one = "A";
        string two = "a";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (one == two)
            {
                MessageBox.Show("Pass");
            }
            else
            {
                MessageBox.Show("Fail");
            }
        }
    }

Thanks in advance

like image 845
user2953063 Avatar asked Dec 15 '22 01:12

user2953063


1 Answers

You could use this

string.Equals(one, two, StringComparison.CurrentCultureIgnoreCase)

Your code would be

if (string.Equals(one, two, StringComparison.CurrentCultureIgnoreCase))
{
   MessageBox.Show("Pass");
}
else
{
   MessageBox.Show("Fail");
}


Using CurrentCultureIgnoreCase :

Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.

More info here

like image 65
Justin Iurman Avatar answered Dec 26 '22 18:12

Justin Iurman