Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Escaping String for Storage in csv file

Tags:

java

escaping

csv

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?

like image 307
Christian Avatar asked Jul 26 '10 13:07

Christian


1 Answers

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>
like image 130
JavaSheriff Avatar answered Sep 26 '22 03:09

JavaSheriff