Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Escaping Quotes

Escaping Quotes

Scheduled Pinned Locked Moved C#
questionhelp
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maliary
    wrote on last edited by
    #1

    Hello, Ok, we've gone round and round on this, so let me place an open question. I have a data table where some field values have this ". e.g. Nail 4". I need to replace this with \" as they are bringing error on my excel output. How can I access each field value and replace the " with \" ? Please help me, I'd really appreciate your help.

    Let's do this !

    B G 2 Replies Last reply
    0
    • M maliary

      Hello, Ok, we've gone round and round on this, so let me place an open question. I have a data table where some field values have this ". e.g. Nail 4". I need to replace this with \" as they are bringing error on my excel output. How can I access each field value and replace the " with \" ? Please help me, I'd really appreciate your help.

      Let's do this !

      B Offline
      B Offline
      blackjack2150
      wrote on last edited by
      #2

      string initial = "\""; //just a quote char
      string final = initial.Replace("\"", "\\\"")); //now it's backslash and quote

      M 1 Reply Last reply
      0
      • B blackjack2150

        string initial = "\""; //just a quote char
        string final = initial.Replace("\"", "\\\"")); //now it's backslash and quote

        M Offline
        M Offline
        maliary
        wrote on last edited by
        #3

        Thanks for that, I have it already. But how do I apply this to all the elements in the table ?

        Let's do this !

        M 1 Reply Last reply
        0
        • M maliary

          Thanks for that, I have it already. But how do I apply this to all the elements in the table ?

          Let's do this !

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You need to use foreach and process each row in the table. You may be able to use linq to do an update but I don't know linq yet!

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • M maliary

            Hello, Ok, we've gone round and round on this, so let me place an open question. I have a data table where some field values have this ". e.g. Nail 4". I need to replace this with \" as they are bringing error on my excel output. How can I access each field value and replace the " with \" ? Please help me, I'd really appreciate your help.

            Let's do this !

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            How are you outputting the Excel, what kind of errors do you get, and why do you think that \" should be used to escape the quotation marks? I would rather expect that "" should be used...

            Despite everything, the person most likely to be fooling you next is yourself.

            M 1 Reply Last reply
            0
            • G Guffa

              How are you outputting the Excel, what kind of errors do you get, and why do you think that \" should be used to escape the quotation marks? I would rather expect that "" should be used...

              Despite everything, the person most likely to be fooling you next is yourself.

              M Offline
              M Offline
              maliary
              wrote on last edited by
              #6

              Ok, I am getting data from a datatable and exporting it to an excel sheet. I was using the RKLib.Export library, however this doesn't handle " quotes in the field values. It displays unintelligible data. So I decided to use the function I am asking about to clean out the datatable before exporting it. By the way, do you know where I would get code to export data to excel which handles quotes ?

              Let's do this !

              P G 2 Replies Last reply
              0
              • M maliary

                Ok, I am getting data from a datatable and exporting it to an excel sheet. I was using the RKLib.Export library, however this doesn't handle " quotes in the field values. It displays unintelligible data. So I decided to use the function I am asking about to clean out the datatable before exporting it. By the way, do you know where I would get code to export data to excel which handles quotes ?

                Let's do this !

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                That would also be a clue that what you think is a " is not a ", find out what it is.

                modified on Friday, August 15, 2008 9:52 AM

                1 Reply Last reply
                0
                • M maliary

                  Ok, I am getting data from a datatable and exporting it to an excel sheet. I was using the RKLib.Export library, however this doesn't handle " quotes in the field values. It displays unintelligible data. So I decided to use the function I am asking about to clean out the datatable before exporting it. By the way, do you know where I would get code to export data to excel which handles quotes ?

                  Let's do this !

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  I found very little about RKLib.Export when searching for it. Does it export to CSV? In that case, escaping quotation marks with a backslash will not help you at all. A value in CSV that contains commas (or whatever separator is used), line breaks or quotation marks should be enclosed with quotation marks, and quotation marks are escaped by putting double quotation marks in the value. If the library doesn't handle escaping at all, you can do something like this:

                  if (value.indexOfAny(new char[]{ ',', ';', '"', '\x0d', '\x0a'}) != -1) {
                  value = "\"" + value.Replace("\"", "\"\"") + "\"";
                  }

                  Despite everything, the person most likely to be fooling you next is yourself.

                  M 1 Reply Last reply
                  0
                  • G Guffa

                    I found very little about RKLib.Export when searching for it. Does it export to CSV? In that case, escaping quotation marks with a backslash will not help you at all. A value in CSV that contains commas (or whatever separator is used), line breaks or quotation marks should be enclosed with quotation marks, and quotation marks are escaped by putting double quotation marks in the value. If the library doesn't handle escaping at all, you can do something like this:

                    if (value.indexOfAny(new char[]{ ',', ';', '"', '\x0d', '\x0a'}) != -1) {
                    value = "\"" + value.Replace("\"", "\"\"") + "\"";
                    }

                    Despite everything, the person most likely to be fooling you next is yourself.

                    M Offline
                    M Offline
                    maliary
                    wrote on last edited by
                    #9

                    Here is the complete link : http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx[^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups