Logo 
Search:

C# FAQ

Submit Interview FAQ
Home » Interview FAQ » C#RSS Feeds

How do I do a case-insensitive string comparison?

  Shared By: Felicia Hill    Date: Sep 17    Category: C#    Views: 1184

Answer:

Use the string.Compare method. Its third parameter specifies case sensitivity.

"fred" == "Fred" // false
string.Compare( "fred", "Fred", StringComparison.CurrentCultureIgnoreCase ) == 0 // true
For more control over the comparison, e.g. exotic features like width-sensitivity, consider using System.Globalization.CompareInfo.Compare(), e.g.

CultureInfo.CurrentCulture.CompareInfo.Compare(
"fred", "Fred",
CompareOptions.IgnoreCase |
CompareOptions.IgnoreKanaType |
CompareOptions.IgnoreWidth
);

Share: 
 

Didn't find what you were looking for? Find more on How do I do a case-insensitive string comparison? Or get search suggestion and latest updates.


Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: