如何在 .NET 中替换重音符号(德语)

发布于 2024-07-30 11:49:22 字数 175 浏览 3 评论 0原文

我需要将字符串中的重音符号替换为对应的英文重音符号

,例如

ä = ae

ö = oe

Ö = Oe

ü = ue

我知道从字符串中删除它们,但我不知道替换。

如果您有任何建议,请告诉我。 我正在用 C# 编码

I need to replace accents in the string to their english equivalents

for example

ä = ae

ö = oe

Ö = Oe

ü = ue

I know to strip of them from string but i was unaware about replacement.

Please let me know if you have some suggestions. I am coding in C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

握住你手 2024-08-06 11:49:22

如果您需要在较大的字符串上使用此功能,则多次调用 Replace() 很快就会变得低效。 您最好逐个字符地重建字符串:

var map = new Dictionary<char, string>() {
  { 'ä', "ae" },
  { 'ö', "oe" },
  { 'ü', "ue" },
  { 'Ä', "Ae" },
  { 'Ö', "Oe" },
  { 'Ü', "Ue" },
  { 'ß', "ss" }
};

var res = germanText.Aggregate(
              new StringBuilder(),
              (sb, c) => map.TryGetValue(c, out var r) ? sb.Append(r) : sb.Append(c)
              ).ToString();

If you need to use this on larger strings, multiple calls to Replace() can get inefficient pretty quickly. You may be better off rebuilding your string character-by-character:

var map = new Dictionary<char, string>() {
  { 'ä', "ae" },
  { 'ö', "oe" },
  { 'ü', "ue" },
  { 'Ä', "Ae" },
  { 'Ö', "Oe" },
  { 'Ü', "Ue" },
  { 'ß', "ss" }
};

var res = germanText.Aggregate(
              new StringBuilder(),
              (sb, c) => map.TryGetValue(c, out var r) ? sb.Append(r) : sb.Append(c)
              ).ToString();
万水千山粽是情ミ 2024-08-06 11:49:22

是否只想将德语变音符号映射到两个字母(非变音符号)变体? 干得好; 未经测试,但它可以处理所有德语元音变音。

String replaceGermanUmlauts( String s ) {
    String t = s;
    t = t.Replace( "ä", "ae" );
    t = t.Replace( "ö", "oe" );
    t = t.Replace( "ü", "ue" );
    t = t.Replace( "Ä", "Ae" );
    t = t.Replace( "Ö", "Oe" );
    t = t.Replace( "Ü", "Ue" );
    t = t.Replace( "ß", "ss" );
    return t;
}

Do just want a mapping of german umlauts to the two-letter (non-umlaut) variant? Here you go; untested, but it handles all german umlauts.

String replaceGermanUmlauts( String s ) {
    String t = s;
    t = t.Replace( "ä", "ae" );
    t = t.Replace( "ö", "oe" );
    t = t.Replace( "ü", "ue" );
    t = t.Replace( "Ä", "Ae" );
    t = t.Replace( "Ö", "Oe" );
    t = t.Replace( "Ü", "Ue" );
    t = t.Replace( "ß", "ss" );
    return t;
}
阳光①夏 2024-08-06 11:49:22

我想不出任何自动方法来执行此操作,因此我相信您必须手动执行此操作。

IE。

string GermanString = "äö";
GermanString = GermanString.Replace("ä", "ae");
GermanString = GermanString.Replace("ö", "oe");

有多少个字符? 所有元音,大写和小写,那么,10? 工作量不应该太大。

I can't think of any automatic way to do this, so I believe you'd have to do it manually.

ie.

string GermanString = "äö";
GermanString = GermanString.Replace("ä", "ae");
GermanString = GermanString.Replace("ö", "oe");

How many characters are there? All vowels, in upper and lower case, so, 10? Shouldn't be too much of a job.

許願樹丅啲祈禱 2024-08-06 11:49:22

使用 string.Replace 怎么样:(

string germanText = "Mötörhead";
string replaced = germanText.Replace("ö", "oe");

好吧,不是真正的德语单词,但我无法抗拒)

你可以像这样链接调用 Replace

someText.Replace("ö", "oe").Replace("ä", "ae").Replace("ö", "oe")...

How about using string.Replace:

string germanText = "Mötörhead";
string replaced = germanText.Replace("ö", "oe");

(okay, not a real German word, but I couldn't resist)

You can chain calls to Replace like this

someText.Replace("ö", "oe").Replace("ä", "ae").Replace("ö", "oe")...
难如初 2024-08-06 11:49:22

此类删除变音符号(é、ì、è 等),并用其等效项“ae (ä)”、“oe (ö)”、“ue (ü)”和“ss”替换变音符号和德语“ß” (ß)”。

public sealed class UmlautConverter
{
    private Dictionary<char, string> converter = new Dictionary<char, string>()
    {
        {  'ä', "ae" },
        {  'Ä', "AE" },
        {  'ö', "oe" },
        {  'Ö', "OE" },
        {  'ü', "ue" },
        {  'Ü', "UE" },
        {  'ß', "ss" }
    };

    string value = null;
    public UmlautConverter(string value)
    {
        if (!string.IsNullOrWhiteSpace(value))
        {
            this.value = value;
        }
    }
    public string RemoveDiacritics()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return null;
        }

        string normalizedString = this.value.Normalize();

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            string temp = normalizedString;
            normalizedString = temp.Replace(item.Key.ToString(), item.Value);
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < normalizedString.Length; i++)
        {
            normalizedString = normalizedString.Normalize(NormalizationForm.FormD);
            string c = normalizedString[i].ToString();
            if (CharUnicodeInfo.GetUnicodeCategory(Convert.ToChar(c)) != UnicodeCategory.NonSpacingMark)
            {
                stringBuilder.Append(c);
            }
        }
        return stringBuilder.ToString();
    }

    public bool HasUmlaut()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return false;
        }

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            if (this.value.Contains(item.Key.ToString()))
            {
                return true;
            }
        }

        return false;
    }
}

用法:

Console.WriteLine(new UmlautConverter("Nürnberger Straße").RemoveDiacritics()); // Nuernberger Strasse

        Console.WriteLine(new UmlautConverter("Größenwahn").RemoveDiacritics()); // Groessenwahn
        Console.WriteLine(new UmlautConverter("Übermut").RemoveDiacritics()); // UEbermut
        Console.WriteLine(new UmlautConverter("Università").RemoveDiacritics()); // Universita
        Console.WriteLine(new UmlautConverter("Perché").RemoveDiacritics());// Perche
        Console.WriteLine(new UmlautConverter("être").RemoveDiacritics()); // etre

在“Übermut”情况下有一个小错误,用“UE”代替“Ue”替换“Ü”。但这很容易修复。享受吧:)

This class removes diacritic characters (é, ì, è, etc.) and replaces umlauts and the German "ß" with their equivalents "ae (ä)", "oe (ö)", "ue (ü)" and "ss (ß)".

public sealed class UmlautConverter
{
    private Dictionary<char, string> converter = new Dictionary<char, string>()
    {
        {  'ä', "ae" },
        {  'Ä', "AE" },
        {  'ö', "oe" },
        {  'Ö', "OE" },
        {  'ü', "ue" },
        {  'Ü', "UE" },
        {  'ß', "ss" }
    };

    string value = null;
    public UmlautConverter(string value)
    {
        if (!string.IsNullOrWhiteSpace(value))
        {
            this.value = value;
        }
    }
    public string RemoveDiacritics()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return null;
        }

        string normalizedString = this.value.Normalize();

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            string temp = normalizedString;
            normalizedString = temp.Replace(item.Key.ToString(), item.Value);
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < normalizedString.Length; i++)
        {
            normalizedString = normalizedString.Normalize(NormalizationForm.FormD);
            string c = normalizedString[i].ToString();
            if (CharUnicodeInfo.GetUnicodeCategory(Convert.ToChar(c)) != UnicodeCategory.NonSpacingMark)
            {
                stringBuilder.Append(c);
            }
        }
        return stringBuilder.ToString();
    }

    public bool HasUmlaut()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return false;
        }

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            if (this.value.Contains(item.Key.ToString()))
            {
                return true;
            }
        }

        return false;
    }
}

Usage:

Console.WriteLine(new UmlautConverter("Nürnberger Straße").RemoveDiacritics()); // Nuernberger Strasse

        Console.WriteLine(new UmlautConverter("Größenwahn").RemoveDiacritics()); // Groessenwahn
        Console.WriteLine(new UmlautConverter("Übermut").RemoveDiacritics()); // UEbermut
        Console.WriteLine(new UmlautConverter("Università").RemoveDiacritics()); // Universita
        Console.WriteLine(new UmlautConverter("Perché").RemoveDiacritics());// Perche
        Console.WriteLine(new UmlautConverter("être").RemoveDiacritics()); // etre

There is a minor bug in the "Übermut" case replacing "Ü" with "UE" instead of Ue". But this can be easily fixed. Enjoy :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文