EN
Java - csv escape comma
4 points
Quick solution:
xxxxxxxxxx
1
import org.apache.commons.lang3.StringEscapeUtils;
2
3
public class EscapeCsv {
4
5
public static void main(String[] args) {
6
String escaped = StringEscapeUtils.escapeCsv("test1,test2,test3;test4");
7
System.out.println(escaped); // "test1,test2,test3;test4"
8
}
9
}
Maven dependency:
xxxxxxxxxx
1
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
2
<dependency>
3
<groupId>org.apache.commons</groupId>
4
<artifactId>commons-lang3</artifactId>
5
<version>3.9</version>
6
</dependency>
Next example:
xxxxxxxxxx
1
import org.apache.commons.lang3.StringEscapeUtils;
2
3
public class EscapeCsv {
4
5
public static void main(String[] args) {
6
String input = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" +
7
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36";
8
9
String escaped = StringEscapeUtils.escapeCsv(input);
10
11
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
12
System.out.println(escaped);
13
}
14
}
Note:
StringEscapeUtils was moved from org.apache.commons.lang3 to org.apache.commons.text that's why class StringEscapeUtils is depricated.
Here you can find org.apache.commons.text maven dependency: