简体   繁体   中英

Split one column to multiple columns

I have one column contains below data. want to split data in to multiple columns using java code. problem i am facing was in string I have double quotes with comma it was falling in to another column. I have to split data as follows(target). Can any one help to fix this?

I/P: Column:

abc,"test,data",valid

xyz,"sample,data",invalid

Target:

Col1|Col2|Col3

abc|"test,data"|valid

xyz|"sample_data"|invalid

I highly recommend that you use a library to handle instead doing it yourself. I guess your data is in CSV format, so you should take a look at common-csv.

You can resolve your problem with simple code:

CSVParser records = CSVParser.parse("abc,\"test,data\",valid", CSVFormat.DEFAULT);
for (CSVRecord csvRecord : records) {
    for (String value : csvRecord) {
        System.out.println(value);
    }
}

Output:

abc
test,data
valid

Read more at https://www.baeldung.com/apache-commons-csv

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