Search code examples
apex-code

escaping double quote and comma for to generate CSV


Here is the data that I need to parse them as CSV.

Actually, I am making CSV string which I will need to import to another system. Basically, I append comma between each field that I query from DB.

Column1 :
Testing

Column 2: 
<p class="MsoNormal" style=""><b><span style="font-size: 10.0pt; ">This is just a test, test2, test3</span></b><span style="font-size: 10.0pt; "></span></p>

Column 3:
Blah Blah

Now, I am facing problem of retaining double quotes and comma (as I need to save as in HTML format of this text).

I try to append double quote for for the column2 data at the start and end, but it doesn't work out.

Any suggestion for this?


Solution

  • String has the instance method escapeCSV, this should be what you need.

    If you need something different you could always use replace to replace any characters you want to escape with escapeCharacter+originalCharacter eg. (" => \").