我如何在 .NET 中替换口音(德语) [英] How do i replace accents (german) in .NET

查看:23
本文介绍了我如何在 .NET 中替换口音(德语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如

ä = ae

ö = oe

Ö = Oe

ü = ue

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

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

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

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

推荐答案

如果您需要在较大的字符串上使用它,多次调用 Replace() 会很快变得效率低下.您最好逐个字符地重建字符串:

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

这篇关于我如何在 .NET 中替换口音(德语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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