简体   繁体   中英

Double Quote not close Java

J have CSV File first row is like this:

LOC;11000;"Autorisation "valide";10;;

When i try to read it with CSVReader i have only this:

LOC;11000

I know the problem is the double quote, how i can do to remove all double quote on my CSV?

I try replace method is dosnt work.

My code is:

while ((line = reader.readNext()) != null) {

    for(int i = 0; i<line.length; i++) {

        if (line[i].contains("\"")){

            line[i] = line[i].replace("\"", ""); // same result with replaceAll method
        }

        System.out.print(line[i]+" ");
    }
}

Thank you.

As mentioned by Brendan, the line " if (line[i].contains(""")){" is redundant. The find and replace method will scan the text anyways, you don't need to do it twice.

Your replace statements works, I tried it, but that is not the issue:

jshell> var text = "LOC;11000;\"Autorisation \"valide\";10;;";
text ==> "LOC;11000;\"Autorisation \"valide\";10;;"

jshell> text.contains("\"")
$2 ==> true

jshell> text.replace("\"","");
$3 ==> "LOC;11000;Autorisation valide;10;;"

Notice that line is not a list/iteratable, its a string, so you can just replace it on line.

while ((line = reader.readNext()) != null) {
     line = line.replaceAll("[\"\\\\]","");  
     System.out.print(line);
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM