简体   繁体   中英

How to merge many List<String> elements in one based on double quote delimiter in java

I have a CSV file generated in other platform ( Salesforce ), by default it seems Salesforce is not handling break lines in the file generation in some large text fields, so in my CSV file I have some rows with break lines like this that I need to fix:

"column1","column2","my column with text
here the text continues

more text in the same field
here we finish this","column3","column4"

Same idea using this piece of code:

        List<String> listWords = new ArrayList<String>();
        listWords.add("\"Hi all");
        listWords.add("This is a test");
        listWords.add("of how to remove");
        listWords.add("");
        listWords.add("breaklines and merge all in one\"");
        listWords.add("\"This is a new Line with the whole text in one row\"");

in this case I would like to merge the elements. My first approach was to check for the lines were the last char is not a ("), concatenates the next line and just like that until we see the las char contains another double quote.

this is a non working sample of what I was trying to achieve but I hope it gives you an idea

            String[] csvLines = csvContent.split("\n"); 

            Integer iterator = 0;
            String mergedRows = "";

            for(String row:csvLines){
                newCsvfile.add(row);
                if(row != null){
                    if(!row.isEmpty()){
                        String lastChar = String.valueOf(row.charAt(row.length()-1));                       
                        if(!lastChar.contains("\"")){                           
                            //row += row+" "+csvLines[iterator+1].replaceAll("\r", "").replaceAll("\n", "").replaceAll("","").replaceAll("\r\n?|\n", "");
                            mergedRows += row+" "+csvLines[iterator+1].replaceAll("\r", "").replaceAll("\n", "").replaceAll("","").replaceAll("\r\n?|\n", "");
                            row = mergedRows;
                            csvLines[iterator+1] = null;
                        }
                    }
                    newCsvfile.add(row);                    
                }   
                iterator++; 
            }

My final result should look like (based on the list sample):

"Hi all This is a test of how to remove break lines and merge all in one"

"This is a new Line with the whole text in one row".

What is the best approach to achieve this?

In case you don't want to use a CSV reading library like @RealSkeptic suggested...

Going from your listWords to your expected solution is fairly simple:

List<String> listSentences = new ArrayList<>(); 
String tmp = "";

for (String s : listWords) {
    tmp = tmp.concat(" " + s);
    if (s.endsWith("\"")){
        listSentences.add(tmp);
        tmp = "";
    }
}

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