4

I want to write a F# or C# function that removes Spanish accents from a string like so:

in: "a stríng withóut áccents"
->  
out: "a string without accents"

I know how to achieve this in Python 3:

trans_table =  str.maketrans( "áéíóúñÁÉÍÓÚÑàèìòùäëïöü", "aeiounAEIOUNaeiouaeiuo" )
# trans_table now contains a dictionary: { "á" : "a", "é" : "e", ... }
"A stríng withóut áccents".translate( trans_table )
# result is: A string without accents 

Mimicking this solution would be rather straight forward if the characters to be translated where regular ascii characters (in the 0-127 range). However, because of the way .NET encodes strings it doesn't seem straight forward to do it for actual unicode characters out of this range...

I would like a solution that does not imply doing a regex-replace for each one of the many accented characters but that hopefully loops over the string only once...

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mateo
  • 1,494
  • 1
  • 18
  • 27

0 Answers0