C# Remove Diacritics

Date: 2020-08-10
public static string RemoveDiacritics(string text)
{
	var normalizedString = text.Normalize(NormalizationForm.FormD);
	var stringBuilder = new StringBuilder();

	foreach (var c in normalizedString)
	{
		var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);

		if (unicodeCategory != UnicodeCategory.NonSpacingMark)
		{
			stringBuilder.Append(c);
		}
	}

	return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}

public static string RemoveSpecialCharacters(string str)
{
	StringBuilder sb = new StringBuilder();
	foreach (char c in str)
	{
		if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
		{
			sb.Append(c);
		}
	}
	return sb.ToString();
}
38840cookie-checkC# Remove Diacritics