If want to store user created strings in a csv file. Is there a preferred library to use for Escaping the string or should I write my own function?
For anyone looking for code:
add this to your pom.xml
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Then use:
String escaped = StringEscapeUtils.escapeCsv("tHIS String 'needs escaping'");
System.out.println(escaped); //safe for csv
UPD: as of version 3.6, StringEscapeUtils
in commons-lang
deprecated, so you have to use commons-text
instead:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With