views:

394

answers:

4

Hi Friends,

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#

+2  A: 

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.

Paul McLean
A: 

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")...
Mark Seemann
+3  A: 

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;
}
Frerich Raabe
+1  A: 

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) =>
              {
                string r;
                if(map.TryGetValue(c, out r))
                  return sb.Append(r);
                else
                  return sb.Append(c);
              }).ToString();
dahlbyk