4

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

user2953063
  • 437
  • 2
  • 6
  • 10

7 Answers7

10

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

Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
3
if (string.Equals(one, two, StringComparison.CurrentCultureIgnoreCase))

From MSDN:

StringComparer.CurrentCultureIgnoreCase Property

Gets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

Various options:

if (String.Compare(one, two, StringComparison.CurrentCultureIgnoreCase) == 0) {
   // they are equal
}

Option 2:

if ((one ?? "").ToLower() == (two ?? "").ToLower())
   // they are equal
}

There are tons of other options, but these should get you started!

NOTE - One thing people regularly forget with string comparisons is null values. Be sure to watch for null values however you do your comparison. The second option I presented does an excellent job of this.

drew_w
  • 10,320
  • 4
  • 28
  • 49
0

Use a case insensitive string comparer:

if(StringComparer.OrdinalIgnoreCase.Equals(one, two)) 

You should also consider if this comparison needs to be done in the user's culture.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54
0

You should use .Equals() to compare strings. Then you can use StringComparison.OrdinalIgnoreCase as the third argument to ignore case.

m4tt1mus
  • 1,642
  • 14
  • 24
0

You can as well use this

string one = "obi";
 string two = "Obi";

 if(one.Equals(two, StringComparison.OrdinalIgnoreCase))
{
   /* Your code */
}
Cizaphil
  • 490
  • 1
  • 6
  • 23
-2
if(one.ToLower() == two.ToLower())
{
    //Do stuff
}
Logarr
  • 2,120
  • 1
  • 17
  • 29
  • 1
    1) this code doesn't compile. 2) it violates the premise of the question, which is bolded. – Colin DeClue Feb 18 '14 at 21:16
  • @ColinDeClue Oh sorry, I forgot the `()`'s. And calling ToLower will not change the actual variables, so if the OP were to use the variables later they would appear unchanged. – Logarr Feb 18 '14 at 21:46