How to compare strings with case insensitive and accent insensitive
Asked Answered
I

2

10

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İ 
Illona answered 12/1, 2015 at 13:51 Comment(1)
Take a look at this answer #360327Am‚lie
J
16

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));
Jabalpur answered 12/1, 2015 at 13:57 Comment(4)
ok this method failed for these 2 strings : tarafli - TARAFLİ however SQL server says equal as supposed to beCelia
@MonsterMMORPG What locale are those strings from?Jabalpur
@MonsterMMORPG It works fine when I try it with those strings. I'll put it in my answer.Jabalpur
I've tried it too and it works for me (I used the invariant culture).Aponeurosis
V
5

You can use string.Compare with the overload which takes the proper CultureInfo and CompareOptions:

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

Edit:

As for your question on CultureInfo, from MSDN:

The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.

Vaginectomy answered 12/1, 2015 at 13:56 Comment(6)
Because you want to be compatible with the culture the user runs on his machine. See my edit.Vaginectomy
thanks for answer again can we say that MS SQL server also uses that information when i set invariant culture and invariant case ?Celia
What does MSSQL have to do with anything?Vaginectomy
whichever machine it works invariant culture and invariant case so i wondered :D ah i set latin though :D i see now tyCelia
ok this method failed for these 2 strings : tarafli - TARAFLİ however SQL server says equal as supposed to beCelia
Example: string.Compare("citroen","Citroën", CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) 0Torruella

© 2022 - 2024 — McMap. All rights reserved.