C# 字符串比较 'ö' 'oe' 'o' [英] C# string comparision 'ö' 'oe' 'o'

查看:27
本文介绍了C# 字符串比较 'ö' 'oe' 'o'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何识别拼写不同的相似词

我试图在比较这 3 个字符串时返回 true:'voest'、'vost' 和 'vöst'(德国文化),因为它是同一个词.(实际上,只有 oe 和 ö 是相同的,但例如,对于 DB 归类 CI,它是相同的,这是正确的,因为 'vost' 是错误输入的 'voest')

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(..) 总是返回 false,无论我向该方法提供什么参数.

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

如何让 string.Compare()/Equals(..) 返回 true ?

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

推荐答案

您可以创建一个忽略变音符号的自定义比较器:

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();
    }
}

现在您可以将此比较器与 Enumerable 方法一起使用,例如 不同:

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

这篇关于C# 字符串比较 'ö' 'oe' 'o'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆