How can I convert á
to a
in C#?
For instance: aéíúö
=> aeiuo
Um, having read those threads [I didn't know they were called diatrics, so I couldn't possible search for that].
I want to "drop" all diatrics but ñ
Currently I have:
public static string RemoveDiacritics(this string text)
{
string normalized = text.Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (char c in from c in normalized
let u = CharUnicodeInfo.GetUnicodeCategory(c)
where u != UnicodeCategory.NonSpacingMark
select c)
{
sb.Append(c);
}
return sb.ToString().Normalize(NormalizationForm.FormC);
}
What would be the best way to leave ñ
out of this?
My solution was to do the following after the foreach:
var result = sb.ToString();
if (text.Length != result.Length)
throw new ArgumentOutOfRangeException();
int position = -1;
while ((position = text.IndexOf('ñ', position + 1)) > 0)
{
result = result.Remove(position, 1).Insert(position, "ñ");
}
return sb.ToString();
But I'd assume there is a less "manual" way to do this?