Languages
[Edit]
EN

Java - csv escape comma

4 points
Created by:
ParaEagle
484

Quick solution:

import org.apache.commons.lang3.StringEscapeUtils;

public class EscapeCsv {

    public static void main(String[] args) {
        String escaped = StringEscapeUtils.escapeCsv("test1,test2,test3;test4");
        System.out.println(escaped); // "test1,test2,test3;test4"
    }
}

Maven dependency:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.9</version>
</dependency>

Next example:

import org.apache.commons.lang3.StringEscapeUtils;

public class EscapeCsv {

    public static void main(String[] args) {
        String input = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" +
                " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36";

        String escaped = StringEscapeUtils.escapeCsv(input);
        
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
        System.out.println(escaped);
    }
}

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:

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join