Pratiyush Kumar Singh
Pratiyush Kumar Singh

Reputation: 2007

Apache CSV not working properly when withIgnoreSurroundingSpaces is used

While using Apache CSV with below maven dependency. I am getting un expected results.

  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.2</version>
</dependency>

and code

    String test = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)
                  .format(""," "," Test1","Test2","   Test3     ");
    System.out.println(test);

Actual Output

""," "," Test1",Test2,"   Test3     "

Expected Output

"","","Test1","Test2","Test3"

Errors in output: 1. Extra space is not trimmed. 2. Test2 is printed without quotes is it normal behavior.

Upvotes: 2

Views: 291

Answers (1)

grigouille
grigouille

Reputation: 705

IgnoreSurroundingSpaces ignore spaces around the double quotes. It does not trim inside the double quotes. What you want is :

String test = CSVFormat.DEFAULT.builder().setTrim(true).setQuoteMode(QuoteMode.ALL).build()
.format(""," "," Test1","Test2","   Test3     ");
System.out.println(test);

Upvotes: 0

Related Questions