Created
March 13, 2014 15:21
-
-
Save dhlavaty/9530474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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