Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Creating CSV file where values contain comma Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating CSV file where values contain comma" Watch "Creating CSV file where values contain comma" New topic
Author

Creating CSV file where values contain comma

dinesh chanchlani
Greenhorn

Joined: Jul 20, 2007
Posts: 6
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...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
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?


Ping & DNS - my free Android networking tools app
 
 
subject: Creating CSV file where values contain comma
 
Similar Threads
CSV - Difficulty in having the delimiter recognized
Creating a CSV File from a HashMap
Saving data to Excel files
how to write comma in CSV file
uploading csv file into mysql database using jsp