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())
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())
use this:
var result = String.Compare("AA", "aa", StringComparison.OrdinalIgnoreCase);
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.
Well, you can use String.Equals(String,StringComparison)
method. Just pass it StringComparison.InvariantCultureIgnoreCase
or StringComparison.CurrentCultureIgnoreCase
depending on your objectives...
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.
txt_SecAns.Trim().Compare(hidden_secans.Trim(), StringComparison.CurrentCultureIgnoreCase)
string.Compare(string1, string2, true) == 0 will compare if the two strings are equal ignoring case
Use StringComparison.CurrentCultureIgnoreCase
:
if (txt_SecAns.Text.Trim().Equals(hidden_secans.Value.ToString(), StringComparison.CurrentCultureIgnoreCase))
String.Compare(str1, str2, true);
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();
}