Remplacement de caractères spéciaux

alphacrash Messages postés 55 Date d'inscription jeudi 7 novembre 2002 Statut Membre Dernière intervention 17 août 2012 - 9 juil. 2008 à 12:07
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 13 juil. 2008 à 21:15
Voilà une fonction convertir_special() que j'utilise comme ceci: textbox5.text=convertir_special(titre), titre étant une chaine vous vous en doutez.
Lors que je l'exécute, j'ai une exception de type :Object reference not set to an instance of an object. sur la première ligne de ma fonction...au premier remplacement...:-( need help les gens merci' à l'avance...

private string convertir_special(string text)
        {
         
            text = text.Replace("&", "&");
            text = text.Replace("¡", "¡");
            text = text.Replace("¢", "¢");
            text = text.Replace("£", "£");
            text = text.Replace("¤", "¤");
            text = text.Replace("¥", "¥");
            text = text.Replace("¦", "¦");
            text = text.Replace("§", "§");
            text = text.Replace("¨", "¨");
            text = text.Replace("©", "©");
            text = text.Replace("ª", "ª");
            text = text.Replace("«", "«");
            text = text.Replace("¬", "¬");
            text = text.Replace("­", "­");
            text = text.Replace("®", "®");
            text = text.Replace("¯", "¯");
            text = text.Replace("°", "°");
            text = text.Replace("±", "±");
            text = text.Replace("²", "²");
            text = text.Replace("³", "³");
            text = text.Replace("´", "´");
            text = text.Replace("µ", "µ");
            text = text.Replace("¶", "¶");
            text = text.Replace("·", "·");
            text = text.Replace("¸", "¸");
            text = text.Replace("¹", "¹");
            text = text.Replace("º", "º");
            text = text.Replace("»", "»");
            text = text.Replace("¼", "¼");
            text = text.Replace("½", "½");
            text = text.Replace("¾", "¾");
            text = text.Replace("¿", "¿");
            text = text.Replace("À", "À");
            text = text.Replace("Á", "Á");
            text = text.Replace("Â", "Â");
            text = text.Replace("Ã", "Ã");
            text = text.Replace("Ä", "Ä");
            text = text.Replace("Å", "Å");
            text = text.Replace("Æ", "Æ");
            text = text.Replace("Ç", "Ç");
            text = text.Replace("È", "È");
            text = text.Replace("É", "É");
            text = text.Replace("Ê", "Ê");
            text = text.Replace("Ë", "Ë");
            text = text.Replace("Ì", "Ì");
            text = text.Replace("Í", "Í");
            text = text.Replace("Î", "Î");
            text = text.Replace("Ï", "Ï");
            text = text.Replace("Ð", "Ð");
            text = text.Replace("Ñ", "Ñ");
            text = text.Replace("Ò", "Ò");
            text = text.Replace("Ó", "Ó");
            text = text.Replace("Ô", "Ô");
            text = text.Replace("Õ", "Õ");
            text = text.Replace("Ö", "Ö");
            text = text.Replace("×", "×");
            text = text.Replace("Ø", "Ø");
            text = text.Replace("Ù", "Ù");
            text = text.Replace("Ú", "Ú");
            text = text.Replace("Û", "Û");
            text = text.Replace("Ü", "Ü");
            text = text.Replace("Ý", "Ý");
            text = text.Replace("Þ", "Þ");
            text = text.Replace("ß", "ß");
            text = text.Replace("à", "à");
            text = text.Replace("á", "á");
            text = text.Replace("â", "â");
            text = text.Replace("ã", "ã");
            text = text.Replace("ä", "ä");
            text = text.Replace("å", "å");
            text = text.Replace("æ", "æ");
            text = text.Replace("ç", "ç");
            text = text.Replace("è", "è");
            text = text.Replace("é", "é");
            text = text.Replace("ê", "ê");
            text = text.Replace("ë", "ë");
            text = text.Replace("ì", "ì");
            text = text.Replace("í", "í");
            text = text.Replace("î", "î");
            text = text.Replace("ï", "ï");
            text = text.Replace("ð", "ð");
            text = text.Replace("ñ", "ñ");
            text = text.Replace("ò", "ò");
            text = text.Replace("ó", "ó");
            text = text.Replace("ô", "ô");
            text = text.Replace("õ", "õ");
            text = text.Replace("ö", "&ö");
            text = text.Replace("÷", "&÷");
            text = text.Replace("ø", "ø");
            text = text.Replace("ù", "ù");
            text = text.Replace("ú", "ú");
            text = text.Replace("û", "û");
            text = text.Replace("ü", "ü");
            text = text.Replace("ý", "ý");
            text = text.Replace("þ", "þ");
            text = text.Replace("ÿ", "ÿ");
           // MessageBox.Show(text);
            return text;
        }

Coding is not a crime!

4 réponses

Tilois Messages postés 721 Date d'inscription dimanche 10 juin 2001 Statut Membre Dernière intervention 27 mars 2011 7
9 juil. 2008 à 12:23
il faut faire tes replaces seulement si :!string.IsNullOrEmpty(Text)
En effet les replace dans les chaines null ne fonctionnent pas!
0
SharpMao Messages postés 1024 Date d'inscription mardi 4 février 2003 Statut Membre Dernière intervention 7 juin 2010 69
9 juil. 2008 à 12:31
Hello,

ta variable text doit sasn doute être null en arrivant dans ta méthode, d'où le problème.

Dit en passant, il existe une méthode System.Web.HttpUtility.HtmlEncode qui fait à peu de choses prêt la même chose.

Une des seules différences, c'est qu'elle encode "&" par "&".

Si ça te convient, utilise-là plutôt que de la réinventer. Au moins, tu sera sur de ne pas avoir oublié un charactère dans le tas.

Amicalement, SharpMao

"C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!"
(Coluche / 1944-1986 / Pensées et anecdotes)
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
9 juil. 2008 à 13:01
Et la le gros probleme, c'est que chaque appel de Replace recrée une instance de string... qui devra être ensuite Garbage collectée.
Niveau perf, c'est pas terrible

Mx
MVP C# 
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 juil. 2008 à 21:15
Salut,

"Au moins, tu sera sur de ne pas avoir oublié un charactère dans le tas."
Ce n'est pas tout à fait vrai avec System.Web.HttpUtility.HtmlEncode, elle fonctionne sur un principe de blacklist au lieu d'une whitelist qui est plus de rigueur dans ce genre de cas (j'avais abordé le sujet ici) :-).
Il vaut mieux passer par l'AntiXssLibrary donc (et surtout pas un enchainement de Replace).

/*
coq
MVP Visual C#
CoqBlog
*/
0
Rejoignez-nous