Ignore case and compare in C#

Murthy  picture Murthy · Nov 23, 2011 · Viewed 46k times · Source

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())

Answer

Davide Piras picture Davide Piras · Nov 23, 2011

use this:

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

String.Compare Method (String, String, Boolean)