Hi, I have to write a csv file for which the delimiter is to be picked up from a config file. Now I am facing a problem if the delimiter is a special character like \t. As the config file is read and values returned as Strings so the \t is getting printed as such without getting converted to a tab spacing. Please let me know if any of you have an idea on how to tackle this situation. :roll:
Interesting. I think you'll have to test for the "slash followed by another character" delimiter and convert it. You might make a map of supported characters, or try
dlm = " ".replace(" ", "/" + dlm );
The hard-coded "/" plus the one in the config file makes "//t" which regex will recognize as tab. Maybe. Try it and let me know. If it works it should accept anything that regex can recognize with a leading /
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Given exactly the constraints you've given, I think the easiest way would be to convert the delimiter as read from the config file to the one you need to use. You might do this with a series of replaceAll() calls on the string you read in, or you might set up a HashMap or whatever if you have to do that conversion many times.
Of course, if "\t" is the only escaped delimiter you'll ever see and all others will be single characters, then you only have to do one conversion.
Do you have control of the config file. If so, you might encode the delimiter differently.
If the file is XML, you could use CDATA or a single encoded char for the delimiter: <delimiter> </delimiter>
Could you put the ascii code for the delimiter in the file instead? delimiter=09
You could encode the delimiter using some scheme for which a decoder already exists. For instnce a URLDecoder() will take %09 and convert it to a TAB. (%09 is ok to have as an XML value without escaping any of the chars.)
If the file format is readable by a Properties object, then just use Properties.load(). That automatically does \t conversion.
But I don't know of anything built into the base classes that will convert \t to a TAB, \f to a FORMFEED, etc. in a String that's already in memory without enumerating all the ones you might see in your application.
Stan: dlm = " ".replace(" ", "/" + dlm ); I think you meant replaceAll() or replaceFirst(). Also, I think you were thinking of a '\' instead of a '/'.
Stan: The hard-coded "/" plus the one in the config file makes "//t" which regex will recognize as tab. Maybe. Try it and let me know. If it works it should accept anything that regex can recognize with a leading /
I'm not positive it won't work, but I don't think String addition really works like that. If you change your source code to... dlm = " ".replace(" ", "\" + dlm); ...the compiler would complain that you have an open String literal because it would start think that the \" sequence meant you wanted your string to start quote-space-plus-space-dee-ell-emm-paren-semi. If you change it to... dlm = " ".replace(" ", "\\" + dlm); ...when dlm was read in as backslash-tee, you'll have dlm equals to backslash-backslash-tee. Ok, but then what would you do with it to replace that with a TAB? One of us must be missing something.
Ryan [ May 20, 2005: Message edited by: Ryan McGuire ]
Joined: Jan 29, 2003
Yeah, I was out to lunch on that one. You're right about doubling up the BACK slashes.
If you want to use replaceAll() to replace a backslash followed by a 't' with a tab character, you have to use four backslashes in the first argument:Also, as of JDK 1.5, String has a replace() method that takes two CharSequences in addition to the one that takes two char arguments. If you can use that method, it will cut down on the backslash madness:
Joined: Feb 17, 2005
Thanks a lot Alan!! It works now. However I am not sure what difference the four back slashes make? Can you please explain? Thanks in advance, Sunetra.