Forums Register Login

Creating CSV file where values contain comma

+Pie Number of slices to send: Send
Hi all,

Can someone tell me how to generate a CSV file and handle the case when values contain comma in them.

If i use MyString.replaceAll(",","\,")->this is not allowed, i will have to use MyString.replaceAll(",","\\,"),but finally these two backslash cancel each other and what is left is comma.

I want to create CSV file while exporting data from database and while
importing i will use this file to pump the data,so i want to differentiate
between comma as seperater and comma as part of data.

Please help me on this...
+Pie Number of slices to send: Send
Have you tried a nonsense character instead of the real commas?
Look through the active threads on this forum today and find those about Unicode and Chinese. Try changing all your real commas to one of those Chinese characters they are discussing, then change it back when you extract the data. I don't know whether that will fulfill your requirements, but it isn't hard to try.
+Pie Number of slices to send: Send
 

i will have to use MyString.replaceAll(",","\\,"),but finally these two backslash cancel each other and what is left is comma.



What does "two backslash cancel each other" mean? .... not sure what you want to accomplish but are you trying to change a comma into a backslash followed by a comma?

Keep in mind that a backslash has special meaning to both Java strings and regex replacement strings.

To a regex replacement string, it means that the next character is a literal -- so "\," has the same meaning as ",". If you actually mean the literal backslash, then you need to have "\\," as the regex replacement string. But.... You are not done yet.

A backslash also has special meaning in a java string. So if you want to have a literal backslash to pass to the regex, which in this case, is two literal backslashes, you need to escape that as well.... so if you mean a literal "\," as your replacement, you need to specify "\\\\," as the second parameter in your replaceAll() method call.

Henry
+Pie Number of slices to send: Send
CSV has a few edge cases and escape rules that aren't as simple to code as they look at first. Is there a particular reason you couldn't use one of the existing CSV libraries?
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 13984 times.
Similar Threads
CSV - Difficulty in having the delimiter recognized
uploading csv file into mysql database using jsp
how to write comma in CSV file
Saving data to Excel files
Creating a CSV File from a HashMap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:21:10.