com.glaivestone.toolbox.text.csv
Class CSVParser

java.lang.Object
  |
  +--com.glaivestone.toolbox.text.csv.CSVParser

public class CSVParser
extends java.lang.Object

Parser for processing text in CSV (comma-separated values) format.

A parser is configured with the separator string which separates field values in the text, along with a delimiter string which may be used to enclose field values. The default configuration uses comma as the field value separator and double-quote as the delimiter string.

Leading and trailing white space around a field value is always removed, unless the value is wrapped by the delimiter string so that significant leading or trailing white space is retained. Occurrences of the delimiter within the field value must be doubled-up; these are un-doubled by the parser to return the value containing the embedded delimiter.

The value for a field is always returned as a String instance, possibly empty. A field value returned by the parser is never null.

The standard usage pattern is to instantiate a CSVParser configured with the desired options for the field separator character and the field value delimiter, then use it to parse CSV formatted text lines into the field values. Clients may use either the getValues(String) service to parse the text into a List containing the field values or the getStringValues(String) service to parse the text into a String[] array containing the field values.

Author:
Deb Lewis

Constructor Summary
CSVParser()
          Construct a new CSVParser which parses text tokens separated by the default comma separator string.
CSVParser(java.lang.String aSeparator, char stringDelimiter)
          Construct a new CSVParser which parses text tokens separated by the specified separator string.
 
Method Summary
 java.lang.String[] getStringValues(java.lang.String aString)
          Answer a list of the string values encoded in the CSV formatted string.
 java.util.List getValues(java.lang.String aString)
          Answer a list of the string values encoded in the given CSV formatted string.
 void setDelimiter(char stringDelimiter)
          Specify the character which is used as a token delimiter.
 void setSeparator(java.lang.String aSeparator)
          Specify the token separator.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVParser

public CSVParser()
Construct a new CSVParser which parses text tokens separated by the default comma separator string.


CSVParser

public CSVParser(java.lang.String aSeparator,
                 char stringDelimiter)
Construct a new CSVParser which parses text tokens separated by the specified separator string.

Method Detail

getStringValues

public java.lang.String[] getStringValues(java.lang.String aString)
Answer a list of the string values encoded in the CSV formatted string. Returns an empty list if the line is empty.


getValues

public java.util.List getValues(java.lang.String aString)
Answer a list of the string values encoded in the given CSV formatted string. Returns an empty list if the line is empty.


setSeparator

public void setSeparator(java.lang.String aSeparator)
Specify the token separator.


setDelimiter

public void setDelimiter(char stringDelimiter)
Specify the character which is used as a token delimiter.


GFL 1.1 API