Sital Sitoula
Sital Sitoula

Reputation: 1

Is there a way to fix this string output?

I am reading data from a CSV file and putting the string into an array list called album. One of the string was: The Beatles ("The White Album") and the output when I print it using System.out.print(album.get(index)) was:

"The Beatles (""The White Album"")"

Why are there extra quotation marks at the front and last and at between?

I tried replacing the quotation of the string with \" so that it takes quotation as a string too but the output was:

\"The Beatles (\\"The White Album\\")\

Code I tried:

System.out.print(album.get(index).replace('"','\\"');

Upvotes: 0

Views: 62

Answers (1)

iluxa
iluxa

Reputation: 6969

CSV separates values with commas:

value1,value2,value3

But what if the value itself contains a comma, like say value,3? No problem, says CSV, we'll put in in quotes:

value1,value2,"value,3"

So now the quotes are used to surround values with commas... what do we do if such a value itself contains a quote, like say, value "3"? No problem, says CSV, we'll replace the quote with a double quote:

value1,value2,"value ""3"""

In your case, what you probably want to do is this:

  • If the string you read doesn't start with ", use it as is.
  • If it does start with ":
    • substring it to cut the first and last characters - str.substring(1, str.length() - 1)
    • replace all occurrences of "" with "

Upvotes: 1

Related Questions