C# string comparision 'ö' 'oe' 'o' [duplicate]

ε祈祈猫儿з 提交于 2019-12-24 03:05:41

问题


Possible Duplicate:
how to recognize similar words with difference in spelling

I am trying to get returned true while comparing these 3 strings: 'voest', 'vost' and 'vöst' (German culture), because it is the same word. (In fact, only oe and ö are the same, but e.g. for a DB collation CI it is the same which is correct, because 'vost' is a misstyped 'voest')

string.Compare(..) / string.Equals(..) returns always false no matter what arguments I provide to that method.

How to make string.Compare() / Equals(..) return true ?


回答1:


You could create a custom comparer which ignores umlauts:

class IgnoreUmlautComparer : IEqualityComparer<string>
{
    Dictionary<char, char> umlautReplacer = new Dictionary<char, char>()
    {
        {'ä','a'}, {'Ä','A'},
        {'ö','o'}, {'Ö','O'},
        {'ü','u'}, {'Ü','U'},
    };
    Dictionary<string, string> pseudoUmlautReplacer = new Dictionary<string, string>()
    {
        {"ae","a"}, {"Ae","A"},
        {"oe","o"}, {"Oe","O"},
        {"ue","u"}, {"Ue","U"},
    };

    private IEnumerable<char> ignoreUmlaut(string s)
    {
        char value;
        string replaced = new string(s.Select(c => umlautReplacer.TryGetValue(c, out value) ? value : c).ToArray());
        foreach (var kv in pseudoUmlautReplacer)
            replaced = replaced.Replace(kv.Key, kv.Value);
        return replaced;
    }

    public bool Equals(string x, string y)
    {
        var xChars = ignoreUmlaut(x);
        var yChars = ignoreUmlaut(y);
        return xChars.SequenceEqual(yChars);
    }

    public int GetHashCode(string obj)
    {
        return ignoreUmlaut(obj).GetHashCode();
    }
}

Now you can use this comparer with Enumerable methods like Distinct:

string[] allStrings = new[]{"voest","vost","vöst"};
bool allEqual = allStrings.Distinct(new IgnoreUmlautComparer()).Count() == 1;
// --> true



回答2:


You could try IgnoreNonSpace option in comparing. It won't solve voest - vost, but will help with vost-vöst.

int a = new CultureInfo("de-DE").CompareInfo.Compare("vost", "vöst", CompareOptions.IgnoreNonSpace);
// a = 0; strings are equal.


来源:https://stackoverflow.com/questions/13562304/c-sharp-string-comparision-%c3%b6-oe-o

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