C# FAQ: How perform a case insensitive string comparison

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How perform a case insensitive string comparison?

A case-insensitive string comparison can be performed in C# using the String.Compare method. The third parameter is a boolean value used to specify that case should or should not be ignored. For example, the following comparison will return false:

if ("c#" == "C#")

but, the following Compare will return true:

if (System.String.Compare ("c#", "C#", true) == 0)

For greater control over string comparisons, the System.Globalization.CompareInfo.Compare() method may be used as follows:

CultureInfo.CurrentCulture.CompareInfo.Compare
   (
   "c#", "C#", 
   CompareOptions.IgnoreCase | 
   CompareOptions.IgnoreKanaType | 
   CompareOptions.IgnoreWidth
   );

System.Globalization.CompareInfo.Compare() has many options, some of which are quite esoteric.

See also


Personal tools