17

How to convert the string to uppercase before performing a compare, or is it possible to compare the string by ignoring the case

 if (Convert.ToString(txt_SecAns.Text.Trim()).ToUpper() == 
     Convert.ToString(hidden_secans.Value).ToUpper())
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Murthy
  • 1,502
  • 11
  • 31
  • 45

10 Answers10

32

use this:

var result = String.Compare("AA", "aa", StringComparison.OrdinalIgnoreCase);

String.Compare Method (String, String, Boolean)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
20

Case-insensitive string comparison is done like this in C#:

string.Equals("stringa", "stringb", StringComparison.CurrentCultureIgnoreCase)

Watch out! this code is culture dependant; there are several other options available, see http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx.

Kolky
  • 2,917
  • 1
  • 21
  • 42
  • 3
    Using the Turkish culture this code can return false for `STRINGA` and `stringa`. It's very well possible that the OP wants that, but it's important to be aware that culture influences case insensitive comparisons – CodesInChaos Nov 23 '11 at 13:55
  • 2
    If you want it to be culture invariant than use this `string.Equals("stringa", "stringb", StringComparison.InvariantCultureIgnoreCase)` – Fischermaen Nov 23 '11 at 14:08
  • What's the difference between this and `StringComparison.OrdinalIgnoreCase`? – PatPeter Oct 13 '18 at 23:59
5

Well, you can use String.Equals(String,StringComparison) method. Just pass it StringComparison.InvariantCultureIgnoreCase or StringComparison.CurrentCultureIgnoreCase depending on your objectives...

Dmitry
  • 3,069
  • 1
  • 17
  • 26
4

From MSDN:

String.Compare Method (String, String, Boolean):

public static int Compare(
    string strA,
    string strB,
    bool ignoreCase
)

so in your case:

if( String.Compare(txt_SecAns.Text.Trim(), hidden_secans.Value, true) == 0) 
akoso
  • 620
  • 4
  • 10
3

Just like this:

if (string.Compare(txt_SecAns.Text.Trim(), hidden_secans.Value.ToString(), true) == 0)
{
    // DoSomething
}

The third parameter true tells string.Compare to ignore case.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
3
txt_SecAns.Trim().Compare(hidden_secans.Trim(), StringComparison.CurrentCultureIgnoreCase)
Pike65
  • 562
  • 2
  • 6
3

string.Compare(string1, string2, true) == 0 will compare if the two strings are equal ignoring case

JohnathanKong
  • 1,307
  • 3
  • 21
  • 36
3

Use StringComparison.CurrentCultureIgnoreCase:

if (txt_SecAns.Text.Trim().Equals(hidden_secans.Value.ToString(), StringComparison.CurrentCultureIgnoreCase))
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
3
String.Compare(str1, str2, true);
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I'd prefer a method that explicitly specifies the culture. It's not obvious that this uses the current culture. – CodesInChaos Nov 23 '11 at 13:59
  • Yes, this version uses the current culture according to MSDN. There is an overload with a fourth parameter that lets you specify the culture explicitly. – Tudor Nov 23 '11 at 14:07
2

I would personaly compare with a proper culture like everyone here, but something hasn't been suggested :

public bool CompareStrings(string stringA, string StringB)
{
    return stringA.ToLower() == stringB.ToLower();
}
Tipx
  • 7,367
  • 4
  • 37
  • 59