Skip to content

Instantly share code, notes, and snippets.

@dhlavaty
Created March 13, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhlavaty/9530474 to your computer and use it in GitHub Desktop.
Save dhlavaty/9530474 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.Text;
namespace TC
{
// Removes diacritics from a string
//
// Original version by Michael Kaplan:
// -> http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx
//
// Optimized version by Tommy Carlier:
// -> http://blog.tcx.be/2011/11/recently-i-had-to-write-function-that.html
public static class DiacriticsRemover
{
public static string RemoveDiacritics(this string text)
{
if (text == null) throw new ArgumentNullException("text");
if (text.Length > 0)
{
char[] chars = new char[text.Length];
int charIndex = 0;
text = text.Normalize(NormalizationForm.FormD);
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
chars[charIndex++] = c;
}
return new string(chars, 0, charIndex).Normalize(NormalizationForm.FormC);
}
return text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment