String replace diacritics in C#
Jon Skeet mentioned the following code on a newsgroup...
static string RemoveAccents (string input)
{
string normalized = input.Normalize(NormalizationForm.FormKD);
Encoding removal = Encoding.GetEncoding(Encoding.ASCII.CodePage,
new EncoderReplacementFallback(""),
new DecoderReplacementFallback(""));
byte[] bytes = removal.GetBytes(normalized);
return Encoding.ASCII.GetString(bytes);
}
EDIT
Maybe I am crazy, but I just ran the following...
Dim Input As String = "ŠĐĆŽ-šđčćž"
Dim Builder As New StringBuilder()
For Each Chr As Char In Input
Builder.Append(Chr)
Next
Console.Write(Builder.ToString())
And the output was SDCZ-sdccz
It seems you want to strip off diacritics and leave the base character. I'd recommend Ben Lings's solution here for this:
string input = "ŠĐĆŽ šđčćž";
string decomposed = input.Normalize(NormalizationForm.FormD);
char[] filtered = decomposed
.Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.ToArray();
string newString = new String(filtered);
Edit: Slight problem! It doesn't work for the Đ. The result is:
SĐCZ sđccz