1

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?

Edgar
  • 194
  • 1
  • 12

1 Answers1

2

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 = "";
    }
}
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
  • Good answer which works for the OP's example. You'd need something more sophisticated if there's a chance the last field in a row isn't quoted. – slim May 03 '17 at 15:17
  • Thanks @Robin Topper, for what I'm doing now the code works great, Thanks – Edgar May 03 '17 at 15:24