C# If Equals Case insensitive
This question already has an answer here:
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
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.
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.
链接地址: http://www.djcxy.com/p/21034.html上一篇: System.Int32和int的MSIL是否一样?
下一篇: C#如果等于大小写不敏感