public final class CsvEscape extends Object
Utility class for performing CSV escape/unescape operations.
FeaturesSpecific features of the CSV escape/unescape operations performed by means of this class:
There are two different input/output modes that can be used in escape/unescape operations:
In order for Microsoft Excel to correcly open a CSV file —including field values with line breaks— these rules should be followed:
(Note unbescape will perform escaping of field values only, so it will take care of enclosing in double-quotes, using unix-style line breaks inside values, etc. But separating fields (e.g. with ;), delimiting records (e.g. with \r\n) and using the correct character encoding when writing CSV files will be the responsibility of the application calling unbescape.)
The described format for Excel is also supported by OpenOffice.org Calc (File -> Open...) and also Google Spreadsheets (File -> Import...)
ReferencesThe following references apply:
Modifier and Type | Method and Description |
---|---|
static void |
escapeCsv(char[] text,
int offset,
int len,
Writer writer)
Perform a CSV escape operation on a char[] input.
|
static String |
escapeCsv(String text)
Perform a CSV escape operation on a String input.
|
static void |
unescapeCsv(char[] text,
int offset,
int len,
Writer writer)
Perform a CSV unescape operation on a char[] input.
|
static String |
unescapeCsv(String text)
Perform a CSV unescape operation on a String input.
|
public static String escapeCsv(String text)
Perform a CSV escape operation on a String input.
This method is thread-safe.
text
- the String to be escaped.public static void escapeCsv(char[] text, int offset, int len, Writer writer) throws IOException
Perform a CSV escape operation on a char[] input.
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static String unescapeCsv(String text)
Perform a CSV unescape operation on a String input.
This method is thread-safe.
text
- the String to be unescaped.public static void unescapeCsv(char[] text, int offset, int len, Writer writer) throws IOException
Perform a CSV unescape operation on a char[] input.
This method is thread-safe.
text
- the char[] to be unescaped.offset
- the position in text at which the unescape operation should start.len
- the number of characters in text that should be unescaped.writer
- the java.io.Writer to which the unescaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occursCopyright © 2015 The UNBESCAPE team. All rights reserved.