我需要将字符串中的重音符号替换为其对应的英文
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 中替换口音(德语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!