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#
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();
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With