Try the following extension method
public static class StringExtensions
{
public static string SinTildes(this string texto) =>
new String(
texto.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.ToArray()
)
.Normalize(NormalizationForm.FormC);
}
Explanation:
Characters such as á
, ö
, etc can be expressed in Unicode in two ways: A single character that corresponds to the character already accentuated: á
for example or two consecutive characters where the first character is the tilde and the next the character to which ´a
is going to be applied. Both forms are for text editors to show this version - > á
This line:
.texto.Normalize(NormalizationForm.FormD)
Ensures that the string expands to separates the characters as tides and other modifiers in their consituting characters.
Then
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
He makes sure to keep only those characters that are not diacritics.
Then a new string is created with characters already removed
new String(...)
Finally the chain is returned to its normal state with this line
.Normalize(NormalizationForm.FormC)