Enlever les accents d'un string

SharpMao Messages postés 1024 Date d'inscription mardi 4 février 2003 Statut Membre Dernière intervention 7 juin 2010 - 2 nov. 2004 à 10:12
Whismeril Messages postés 19020 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 15 avril 2024 - 1 avril 2014 à 07:08
Hello,

Je cherche un moyen d'enlever les accents d'un string, si possible sans passer par du ASCIIEncoding, qui met d'autres charactères à la place.
En gros, j'aimerais une méthode
RemoceAccents("éèàöüä");
qui me retourne "eeaoua".

Les caractères acentués ne sont qu'un exemple, j'aimerais que ça fonctionne quel que soit la langue.

Amicalement, SharpMao
A voir également:

11 réponses

digital3d Messages postés 37 Date d'inscription jeudi 29 juillet 2004 Statut Membre Dernière intervention 28 février 2005 1
2 nov. 2004 à 11:32
J'ai fait l'exercice pour le fun, voilà mon code:

static void Main(string[] args)
{
string s = "éàù";
string vData = RemoveAccent(s);
Console.WriteLine(vData);
Console.ReadLine();

}
static string RemoveAccent(string Sentence)
{
char[,] characterCollection = new char[3,2];
characterCollection[0,0] = 'é';
characterCollection[0,1] = 'e';
characterCollection[1,0] = 'à';
characterCollection[1,1] = 'a';
characterCollection[2,0] = 'ù';
characterCollection[2,1] = 'u';

string newSentence = "";
for(int i =0;i < Sentence.Length;i++)
{
string c = Sentence.Substring(i,1);
for(int x=0;x <= characterCollection.GetUpperBound(0);x++)
{
if(c == characterCollection[x,0].ToString())
{
c = characterCollection[x,1].ToString();
break;
}
}
newSentence += c;
}
return newSentence;
}

J'espère que c'est clair ! ;)
1
merci bcp , bonne courage dans votre parcour :)
0
Whismeril Messages postés 19020 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 15 avril 2024 656
1 avril 2014 à 07:08
Bonjour,

voici une solution exhaustive, tous les signes diacritiques sont supprimés:

 /// <summary>
        /// Methode qui supprime les accents et autres signes diacritiques, source http://www.developpez.net/forums/d286030/dotnet/langages/csharp/supprimer-accents-lettre/
        /// </summary>
        /// <param name="stIn"></param>
        /// <returns></returns>
        private string RemoveDiacritics(string stIn)
        {
            string stFormD = stIn.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[ich]);
                }
            }

            return (sb.ToString().Normalize(NormalizationForm.FormC));
        }
0
Rejoignez-nous