How to compare strings with case insensitive and accent insensitive

MonsterMMORPG picture MonsterMMORPG · Jan 12, 2015 · Viewed 8.6k times · Source

How to compare strings with case insensitive and accent insensitive

Alright this is done easily at SQL server

However I would like to do the same at C# .NET 4.5.1.

How can I do that with most proper way?

I mean these 3 strings should return equal when compared

http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html

I need a method that would say these 2 below are same SQL server says they are equal.

 tarafli 
 TARAFLİ 

Answer

Matthew Watson picture Matthew Watson · Jan 12, 2015

To ignore both case AND accents, you can use string.Compare() with both the IgnoreNonSpace AND the IgnoreCase options, like so:

string s1 = "http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html";
string s2 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html";
string s3 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html";

Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
Console.WriteLine(string.Compare(s2, s3, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));

In response to your comments below, this works for tarafli and TARAFLİ too.

The following code prints 0, meaning the strings are equal:

string s1 = "tarafli";
string s2 = "TARAFLİ";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));

And here it is using the Turkish culture (I'm guessing at what the correct culture is). This also prints 0:

string s1 = "tarafli";
string s2 = "TARAFLİ";

var trlocale = CultureInfo.GetCultureInfo("tr-TR");
Console.WriteLine(string.Compare(s1, s2, trlocale, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));