Escape or Unescape String for CSV column data in Java using Apache Commons Text

Introduction

In this post we are going to learn how to use StringEscapeUtils class in Apache Commons Text library to escape a String for CSV file format. If the input String value contains a comma, newline or double quote, then the output String value is returned enclosed in double quotes.

Setup Apache Commons Text in Java project

If you are using Gradle build then add the following dependency configuration into build.gradle file.

compile group: 'org.apache.commons', name: 'commons-text', version: '1.9'

Or add the following dependency XML tag to pom.xml file if you are using Maven build.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.9</version>
</dependency>

Or download commons-text-1.9.jar file from Apache Commons Text download page at commons.apache.org

Java code examples how to use org.apache.commons.text.StringEscapeUtils.escapeCsv()

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsEscapeCsvExample1 {
    public static void main(String[] args) {
        String inputString = "1,23";
        String outputString = StringEscapeUtils.escapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: 1,23
Output String: "1,23"

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsEscapeCsvExample2 {
    public static void main(String[] args) {
        String inputString = "sample \n text";
        String outputString = StringEscapeUtils.escapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: sample 
 text
Output String: "sample 
 text"

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsEscapeCsvExample3 {
    public static void main(String[] args) {
        String inputString = "Sample \"value\" in quotes";
        String outputString = StringEscapeUtils.escapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: Sample "value" in quotes
Output String: "Sample ""value"" in quotes"

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsEscapeCsvExample4 {
    public static void main(String[] args) {
        String inputString = "sample text";
        String outputString = StringEscapeUtils.escapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: sample text
Output String: sample text

Java code examples how to use org.apache.commons.text.StringEscapeUtils.unescapeCsv()

StringEscapeUtils also provides unescapeCsv() method to allow you to unescape data from CSV format.

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsUnescapeCsvExample1 {
    public static void main(String[] args) {
        String inputString = "\"1,23\"";
        String outputString = StringEscapeUtils.unescapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: "1,23"
Output String: 1,23

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsUnescapeCsvExample2 {
    public static void main(String[] args) {
        String inputString = "\"sample \n text\"";
        String outputString = StringEscapeUtils.unescapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: "sample 
 text"
Output String: sample 
 text

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsUnescapeCsvExample3 {
    public static void main(String[] args) {
        String inputString = "\"Sample \"\"value\"\" in quotes\"";
        String outputString = StringEscapeUtils.unescapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: "Sample ""value"" in quotes"
Output String: Sample "value" in quotes

import org.apache.commons.text.StringEscapeUtils;

public class StringEscapeUtilsUnescapeCsvExample4 {
    public static void main(String[] args) {
        String inputString = "sample text";
        String outputString = StringEscapeUtils.unescapeCsv(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Output String: " + outputString);
    }
}
Output:

Input String: sample text
Output String: sample text

Happy Coding 😊