-1

I have CSV File and in that file one column contains several values which are like:

PPG, bonds (AMT, current US$)

PPG, bonds (DIS, current US$)

PPG, bonds (DOD, current US$)

PPG, bonds (INT, current US$)

PPG, bonds (NFL, current US$)

PPG, bonds (NTR, current US$)

PPG, bonds (TDS, current US$), etc.

And at the time splitting it gets splits into different strings. I want them to be as one string. Currently, I'm using

String[] data = line.split(",");

Could anyone help me by telling how I can convert this into single String?

user2004685
  • 9,548
  • 5
  • 37
  • 54
Jay
  • 111
  • 1
  • 4
  • 13
  • 1
    You shouldn't parse csv with String.split, but use a proper [CSV parser](http://stackoverflow.com/questions/843997/csv-parsing-in-java-working-example). – Kenney Feb 20 '16 at 16:08

2 Answers2

0

Here is a workaround; You can replace the first occurrence of , with a new char delimiter which you think is not likely to occur in your String and then do the split based on this new delimiter.

For Example, In the below case I'm taking semi-colon i.e. ; as the new delimiter.

String[] data = line.replaceFirst(",",";").split(";");

Input:

PPG, bonds (AMT, current US$)

Output:

data[0] -> PPG
data[1] -> bonds (AMT, current US$)
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Ok, I agree with your point but how will you apply these in the following rows: "France-FRA-Part time employment, female (% of total female employment)-SL.TLF.PART.FE.ZS" "France-FRA-PNG, bonds (TDS, current US$)-DT.TDS.PNGB.CD" "France-FRA-Communications, computer, etc. (% of service exports, BoP)-BX.GSR.CMCP.ZS" Note: "-" represents new Column data. – Jay Feb 20 '16 at 18:04
  • 1
    *Note: "-" represents new Column data.* What do you mean by this? I can't see it anywhere in your question. I have provided the answer based on what you asked and not what you think you should have asked! – user2004685 Feb 20 '16 at 18:41
0

For the example String, you can use:

line.split(",", 2);
Rahul.B
  • 354
  • 3
  • 18