6

I need to compare two strings in German language to check if they are equal and only differ in the use of umlaute. E.g. "Jörg" should be the same as "Joerg".

So I tried:

var ci = new CultureInfo("de-DE");
int compareResult = ci.CompareInfo.Compare("jörg", "joerg", CompareOptions.IgnoreNonSpace);

as well as

int compareResult = String.Compare("jörg", "joerg", true, ci);

(or are those two equal anyway?)

However, this does not work and will return 1. It is the same for all umlauts ö,ü and ä. If I compare strasseand straße in the same way, this does work and returns 0?!

Thanks for any ideas! This post suggests that mine should work.

Community
  • 1
  • 1
silent
  • 14,494
  • 4
  • 46
  • 86
  • There are not many so why don't you just replace them (`"Jörg".Replace("ö","oe")`)? – Random Dev Apr 24 '15 at 10:39
  • There a lots of workarounds, sure, but I don't see why the direct way does not work. – silent Apr 24 '15 at 10:40
  • [Coinkindez? Please see the discussion here!](http://stackoverflow.com/questions/29845872/why-is-ss-equal-to-the-german-sharp-s-character-%C3%9F/29846396#29846396) TLTR? It doesn't work becasue it shouldn't! `Poet != Pöt` etc.. – TaW Apr 24 '15 at 12:18
  • Not sure what you want to tell us. You are aware, that the discussion you linked was derived from the question here?! ;-) – silent Apr 24 '15 at 12:37
  • Possible duplicate of [C# string comparision 'ö' 'oe' 'o'](https://stackoverflow.com/questions/13562304/c-sharp-string-comparision-%c3%b6-oe-o) – ˈvɔlə May 19 '19 at 18:21

2 Answers2

4

I had the same issue and found no other solution then replacing them e.g. by a extension. As far as i know there is no "direct" solution for this.

public static string ReplaceUmlaute(this string s) 
{
    return s.Replace("ä", "ae").Replace("ö", "oe").Replace("ü", "ue").Replace("Ä", "AE").Replace("Ö", "OE").Replace("Ü", "UE");
}

Result:

int compareResult = String.Compare("jörg".ReplaceUmlaute(), "joerg", true, ci); // 0
fubo
  • 44,811
  • 17
  • 103
  • 137
  • Any idea why? Is this a bug in the framework or by design? If ß works, umlaute for sure should work as well?! – silent Apr 24 '15 at 10:41
  • what about â á à ? They are different characters like ä.I would say ae is a workaround to display those characters where they are not supported. – fubo Apr 24 '15 at 10:41
  • @silent: why do you exptect it to return 0 if you compare `ö` and `oe`, that are completely different strings. The fact that other cultures don't have the `ö` and use `oe` as workaround does not cause it to do the same in germany. So you need some kind of dictionary to map the umlaut with another representation that means the same for you. – Tim Schmelter Apr 24 '15 at 10:44
  • 1
    @TimSchmelter ß and ss is completly the same case - and this does work. So why should ö and oe not work? – silent Apr 24 '15 at 10:47
  • @silent: valid point and to be honest, i have no idea why `ß` and `ss` are treated the same both in german culture or any other(even `CultureInfo.InvariantCulture`). I guess it has to do with the comment [here](https://msdn.microsoft.com/en-us/library/vstudio/cc165449.aspx): _" Linguistically (in Windows), "ss" is equal to the German essetz: 'ß' character in both en-US and de-DE cultures."_ – Tim Schmelter Apr 24 '15 at 10:56
  • Character expansion occurs in cases where one letter represents one character, but that one character sorts as if it were two. For example, ß (eszett) is equivalent to ss in both German/Germany and German/Switzerland locales. However, ß is equivalent to sz in the German/Austria locale. https://msdn.microsoft.com/en-us/library/aa242117%28v=vs.60%29.aspx – fubo Apr 24 '15 at 10:58
  • @fubo Thanks for the link. Still does not entirely convice me, though ;-) But that's not your fault but rather Microsoft's... In German ö is just as equal to oe as ß is to ss I would argue. – silent Apr 24 '15 at 11:03
  • @silent i'm with you but Microsoft didn't implement that. Don't kill the bringer of bad news :-) – fubo Apr 24 '15 at 11:09
  • @fubo I didn't ;-) Upvoted already but want to keep it open a bit longer. Maybe someone has another killer idea... – silent Apr 24 '15 at 11:13
  • @silent: fyi, i've asked a question about [why ”ss” is equal to the german sharp-s character 'ß'](http://stackoverflow.com/questions/29845872/why-is-ss-equal-to-the-german-sharp-s-character-%C3%9F). – Tim Schmelter Apr 24 '15 at 12:47
  • @TimSchmelter yes, I saw that and the answer makes kinda sense – silent Apr 24 '15 at 13:25
0

Hope, .net6 upgrade will help to resolve the issue in invariant mode.

Ref: Culture creation and case mapping in globalization-invariant mode

Ram Krish
  • 1
  • 1