问题描述
我需要将字符串中的重音符号替换为其对应的英文
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();
看我眼色不色