1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public static class StringExtensions
{
private static Dictionary<char, char> _replacementMap;
private static Regex _replacementRegex;
private static MatchEvaluator _replacementCallback;
static StringExtensions()
{
_replacementMap = new Dictionary<char, char>
{
{ 'é', 'e' },
{ 'è', 'e' },
{ 'ê', 'e' },
{ 'ë', 'e' },
{ 'à', 'a' },
{ 'â', 'a' },
{ 'ô', 'o' },
{ 'ö', 'o' },
{ 'î', 'i' },
{ 'ï', 'i' },
{ 'ù', 'u' },
{ 'û', 'u' },
{ 'ü', 'u' },
{ 'ç', 'c' }
};
string chars = new string(_replacementMap.Keys.ToArray());
_replacementRegex = new Regex(string.Format("[{0}]", chars), RegexOptions.Compiled);
_replacementCallback =
m =>
{
char c = m.Value[0];
char newChar;
if (_replacementMap.TryGetValue(c, out newChar))
return newChar.ToString();
return m.Value;
};
}
public static string RemoveDiacritics(this string s)
{
return _replacementRegex.Replace(s, _replacementCallback);
}
} |
Partager