Culture-aware string comparison for Umlaute

一个人想着一个人 提交于 2019-12-01 03:06:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/29845211/culture-aware-string-comparison-for-umlaute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!