withIgnoreLeadingWhiteSpace
Brought to you by:
aruckerjones,
sconway
Looks like .withIgnoreLeadingWhiteSpace(true) does not apply to fields without quotes. Is that right? Parsing data like this:
User Name,Address
Joe,123 main
Bob, 234 asdf
will give you this:
Address(name=Joe, address=123 main)
Address(name=Bob, address= 234 asdf)
I would have expected the leading space before 234 to be removed from the String. Lookls like someone else had a similar question here:
https://stackoverflow.com/questions/15439162/trim-leading-and-trailing-spaces-in-opencsv
Hello Andrew -
The ignore leading whitespace is for spaces outside the quotes.
/**
public CSVParserBuilder withIgnoreLeadingWhiteSpace(
final boolean ignoreLeadingWhiteSpace) {
this.ignoreLeadingWhiteSpace = ignoreLeadingWhiteSpace;
return this;
}
We always take data as gospel and try to present that back completely to the user. But things like withIgnoreLeadingWhiteSapce and withStrictQuotes came about because of different requests for handling where the data actually begins and requests for different levels of configurability.
If you want to have all data trimmed then consider using Processors added in 5.0 - you can see some of the documetation at http://opencsv.sourceforge.net/#processors This allows you to create your own custom code to modify the data however you wish and opencsv will run those processors on the data. For your case I would look at the RowProcessor which allows you to pass in a processor that will be applied to the individual elements in the row. So you could trim every string.
Scott :)
There is one other possibility I would like to forward. If you are populating beans using opencsv, you can adjust the setter methods for the fields in question to always perform a trim before assignment. Setters are honored if present.
Thanks guys. RowProcessor is an easy solution. I have a lot of fields and Lombok is generating my getters/setters so rather not override them unless necessary.