'Make string comparison case-insensitive for .net winforms application [duplicate]
Need to have string comparison case insensitive for .net winforms application. It is not a problem when strings are compared in my code, but I need this everywhere. For ex.: there is combobox with items populated from SQL data, where value member is uppercase string, but entity field bound to this combobox as value is allowed to have value (string) lowercase. Same for the rest elements.
Solution 1:[1]
You cannot change the default comparison for strings in .net. .net is a case sensitive language. It has specific methods for comparing strings using different levels of case sensitivity, but (thank goodness) no global setting.
Solution 2:[2]
You can use this:
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
Or extension method:
public static class StringExtensions
{
public static bool Contains(this string source, string value, StringComparison compareMode)
{
if (string.IsNullOrEmpty(source))
return false;
return source.IndexOf(value, compareMode) >= 0;
}
}
and you can call it like this:
bool result = "This is a try".Contains("TRY",
StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine(result);
Solution 3:[3]
Use
if (string1.ToLower().Equals(string2.ToLower()))
{
#something
}
without the code, there is no other advice i can offer you :/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | PhillipH |
Solution 2 | alaa_sayegh |
Solution 3 | Mong Zhu |