1

I have a simple java code that reads text csv file that contains sentences with double quotations: "sentence1","sentence2","sentence3". I want to read some of these sentences(e.g, sentence 1 and 3). I created a buffer reader and used readLine() then used: tokens = fileLine.split(","); where tokens is an array of strings.

I accessed the sentences I'm interested in using the array index as: tokens[0], tokens[3]. The problem is that, I want the sentences only without the double quotations. But my program saved the sentences with "". How can I improve the parsing technique so I can save the sentences without "" ??

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Mem
  • 23
  • 4
  • 11
  • Use a csv parser (there are plenty, including open source solutions) - it will make your life easier (http://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibly-writing-csv-files). – assylias Jun 21 '12 at 15:08

6 Answers6

3

How about String#replaceAll:

theSentence = theSentence.replaceAll("\"", "");

In newer Java versions, I think from Java 5, you can use String#replace(CharSequence,CharSequence) as well:

theSentence = theSentence.replace("\"", "");

And avoid the overhead of regex

MByD
  • 135,866
  • 28
  • 264
  • 277
0

You could do the following:

s = s.substring(1, s.length()-1);
tokens = s.split("\",\"");

Please note that your implementation parses input line "Hello, world","second sentence" as the array

"Hello
world"
"second sentence"

The above code works only if your lines do not contain (escaped) quotes themselves.

Matt
  • 868
  • 8
  • 12
0

You could use the method String.replaceAll(regex, replacement).

For example:

String s = "hello world";
s = s.replaceAll("o","X");
//s now equals "hellX wXrld"

In your case you would want your regex to be: "\"" And your replacement to be: ""

Michael Freake
  • 1,197
  • 2
  • 14
  • 35
0

If you want to remove just " from beginning and end of string you can do it also this way:

String sentence="\"my sentence\"";
System.out.println(sentence);//out->"my sentence"

sentence = sentence.substring(1,sentence.length()-1);   
System.out.println(sentence);//out->my sentence
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

As a more of an interesting solution than anything, rather than splitting right away, why not do this?

String inputFromCSV; // This would have the value of what you read from the CSV.
inputFromCSV=inputFromCSV.substring(1,sentence.length()-1); 
String[] tokens = inputFromCSV.split("\",\""); // Essentially ","

Actually looking at it, it's not that bad, and will work so long as your file keeps the same format.

Sephallia
  • 396
  • 2
  • 9