HOWTO remove diacritics in a string
When comparing strings you sometimes need to ignore diacritics (accents) like é, ö etc.
The following method removes the diacritics:
With the following extension method you can compare two strings for equality:
static string RemoveDiacritics(string stIn) { string stFormD = stIn.Normalize(NormalizationForm.FormD); StringBuilder sb = new StringBuilder(); for (int ich = 0; ich < stFormD.Length; ich++) { UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]); if (uc != UnicodeCategory.NonSpacingMark) {
sb.Append(stFormD[ich]);
}
}
<span style="color: blue">return </span>(sb.ToString().Normalize(<span style="color: #2b91af">NormalizationForm</span>.FormC));
}
public static bool Equals2(this string source, string toCheck) { return RemoveDiacritics(source).ToLower().Trim() == RemoveDiacritics(toCheck).ToLower().Trim(); }
The following code gives as result ‘True’.
string x = “Azië”; bool equal = x.Equals2(“azie”);