4

I am reading a CSV file and have some values like

field 1   field 2           field 3
1        test case1         expecting one, and \"two\", and three

after reading file into a StringBuilder and converting toString() I split the file content by: string.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");.

On iterating the string I get these values:-

1
test case1
expecting one, and ""two"", and three

How I can I replace two double quotes with single double like this "two"

here is my code:-

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class csvStringParser {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub


          String path = "E:/spc.csv";

            String read = readFile(path);
            System.out.println("content of the file before \" = \n"  +read);



//      System.out.println("content of the file after= \n"  +read);

        String[] tokens = read.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
            for(int i = 0;i<tokens.length;i++) {
                String abc = tokens[i].replace("\"\"", "\"");

           //   if(abc.length()>2){
                if(abc.startsWith("\"") && abc.endsWith("\"")){ 
                abc = abc.substring(1, abc.length()-1);
                }
            //  } 

                System.out.println("> "+abc);
            }

    }

    public static String readFile( String file ) throws IOException {
        BufferedReader reader = new BufferedReader( new FileReader (file));
        String         line = null;
        StringBuilder  stringBuilder = new StringBuilder();
        String         ls = System.getProperty("line.separator");

        while( ( line = reader.readLine() ) != null ) {
            stringBuilder.append( line );
            stringBuilder.append( ls );
        }

        return stringBuilder.toString();
    }

}

2 Answers 2

10

Use String#replace(CharSequence, CharSequence).

String input = "this string \"\"has\"\" double quotes";
String output = input.replace("\"\"", "\"");

http://ideone.com/xPQqL

4
  • Doesn't works for me if I write String read = readFile(path); read.replace("\"\"","\""); Aug 9, 2012 at 19:37
  • I do see it now. No, I don't see anything glaringly obvious. Could you put together an ideone example?
    – Matt Ball
    Aug 9, 2012 at 19:50
  • My code reads a csv file, which I cant provide at ideone. But if you create a csv file (comma delimiter) and put the three field values in a row, it reads the content fine but the two double quotes exists Aug 9, 2012 at 19:56
  • @Matt Ball I got the problem, there was a different set of char sequence given by csv after read...I had to convert \\\"\" to \". Thanks Aug 9, 2012 at 20:18
2
String[] tokens = read.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
for(int i = 0;i<tokens.length;i++)
    tokens[i]=StringEscapeUtils.unescapeCsv(tokens[i]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.