简体   繁体   中英

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('"','\\"');

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 "

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