MSDN > Home page del forum > Visual C# General > Deleting weird characters
Formula una domandaFormula una domanda
 

Con rispostaDeleting weird characters

  • sabato 7 novembre 2009 15.15MartienoS Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hello programmers,

    Im having a problem with reading an XML into a MySQL database
    Every record goes good, but when i have this character: ′ Then it crashes so
    i added this one to my list but still doesn't delete the weird character..


    valueAttribute = valueAttribute.Replace("'", "\\'");
    valueAttribute = valueAttribute.Replace("’", "\\'");
    valueAttribute = valueAttribute.Replace("′", "\\'");
    valueAttribute = valueAttribute.Replace("‘", "\\'");
    valueAttribute = valueAttribute.Replace("′", "\\'");
    valueAttribute = valueAttribute.Replace("′", "");

Risposte

  • sabato 7 novembre 2009 17.48Pawan Mishra Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hello

    Since I am not fully aware of the scenario in which you are trying to replace the single-quote(') character.But if it is related to some database related operation then preferably you should try to use perameterized queries to handle such things.

    Well also I am thinking why in your code you are not able to replace the single quote(').Following statement works fine for me :-

    string strReplace = @"MyString'";
    Console.WriteLine(strReplace.Replace("'",@"\")); --> MyString\

    Please let me know if I have missed something.Also Char Code for single quote is 39.So run your statement with 39 as the char code.
  • lunedì 9 novembre 2009 1.40Yort Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hi,

    You need to replace ' with two ', i.e

    valueAttribute = valueAttribute.Replace("'", "''");

    as suggested already by raihansazal, however there may be other characters or issues that can mess up your code and make it vulnerable to sql injection attacks. To avoid all these issues you should use parameterised queries as suggested by Pawan Mishra... it's the safest and best way.

    The \' isn't working because that's how you 'escape' or 'encode' characters for embedding in a string in C# and c like languages, for sql the standard mechansim is to repeat the character, so ' becomes '' instead.

Tutte le risposte

  • sabato 7 novembre 2009 15.37Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    Hi,

    Try to find the char code of that character with valueAttribute.ToCharArray() method.

    Then you can use the following code.
    valueAttribute = valueAttribute.Replace(Convert.ToChar(60).ToString(), "'");//here 60 is the code of the character.
    
  • sabato 7 novembre 2009 15.45MartienoS Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    How can i know that ′ is char(60)?
  • sabato 7 novembre 2009 15.48Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    As I mentioned above you can use valueAttribute.ToCharArray() at debug while breakpoint is hit.

    Then for example if the character is 5th in your string typevalueAttribute.ToCharArray()[5] as expression to quickwatch window. And quickwatch window will show you the number you should use.

  • sabato 7 novembre 2009 15.52MartienoS Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Im just a starter in this might sound stupid these questions but i really want to learn.

    How can i let those code show in my DOS window?

    Console.WriteLine(Description.ToCharArray()); // This doesnt show every character
  • sabato 7 novembre 2009 15.56Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    You can use the following code.

                foreach (char c in Description.ToCharArray())
                {
                    Console.WriteLine(c + ":" + Convert.ToInt32(c));
                }
    
  • sabato 7 novembre 2009 15.59MartienoS Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Lookin good =D

    Character : 8242
    valueAttribute = valueAttribute.Replace( Convert .ToChar(8242).ToString(), ' \\' );

    But still doesnt replace that character :(



  • sabato 7 novembre 2009 17.48Pawan Mishra Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hello

    Since I am not fully aware of the scenario in which you are trying to replace the single-quote(') character.But if it is related to some database related operation then preferably you should try to use perameterized queries to handle such things.

    Well also I am thinking why in your code you are not able to replace the single quote(').Following statement works fine for me :-

    string strReplace = @"MyString'";
    Console.WriteLine(strReplace.Replace("'",@"\")); --> MyString\

    Please let me know if I have missed something.Also Char Code for single quote is 39.So run your statement with 39 as the char code.
  • domenica 8 novembre 2009 8.20raihansazal Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
     you  can use:
    stingObj = stringObj..Replace("'", "''");
  • lunedì 9 novembre 2009 1.40Yort Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hi,

    You need to replace ' with two ', i.e

    valueAttribute = valueAttribute.Replace("'", "''");

    as suggested already by raihansazal, however there may be other characters or issues that can mess up your code and make it vulnerable to sql injection attacks. To avoid all these issues you should use parameterised queries as suggested by Pawan Mishra... it's the safest and best way.

    The \' isn't working because that's how you 'escape' or 'encode' characters for embedding in a string in C# and c like languages, for sql the standard mechansim is to repeat the character, so ' becomes '' instead.