www.icetips.com  Icetips Utilities Documentation 9/12/2015    

String Class:  ParseCSVLine  (June 29, 2015)

Previous  Top  Next  


Prototype: (STRING pLine, Byte pStringsAreQuoted=TRUE,<String pDelimiter>, <STRING pQuoteChar>),LONG

 

pLineThe line to parse.  It should contain valid CSV data, either comma separated or semicolon separated. 
pStringsAreQuotedIndicates if the strings are quoted with double quotes.  Defaults to TRUE.
pDelimiterOptionally set the delimiter to use (single character).  This is the character that separate the fields in the CSV line.  If it is not specified, comma (,) is used.
pQuoteCharOptionally set the character used to quote strings.  If not specified double quotes (") is used.  Added 2015-09-12

 

ReturnsNumber of fields parsed out of the line.

 

This method takes a string and parses out comma separated (CSV) field data from it.  Normally this data is formatted with commas separating the fields, strings inside double quotes and numbers unquoted.  The method takes into account if the delimiter is inside quoted strings and will ignore them.  This will only work properly in well formatted CSV data string.

 

Example:

 

Parsing a normal comma separated file:

 

ITS  ITStringClass

S    STRING(255)

I    LONG

F    LONG

X    LONG

CODE

ITS.FileToLines('testcsv.csv')

LOOP I = 1 TO Records(ITS.Lines)

   GET(ITS.Lines,I)

   F = ITS.ParseCSVLine(ITS.Lines.OL)

   ITS.ODS('Line: ' & ITS.Lines.OL)

   IF F

     ITS.ODS('  ' & Format(F,@n_3) & ' Fields from ' & ITS.Lines.OL)

     LOOP X = 1 TO F

       GET(ITS.CSVFields,X)

       ITS.ODS('    ' & Format(X,@n02) & ' ' & ITS.CSVFields.OL)

     END

   END

END

 

Parsing a semicolon separated file:

 

ITS  ITStringClass

S    STRING(255)

I    LONG

F    LONG

X    LONG

CODE

ITS.FileToLines('testcsv2.csv')

LOOP I = 1 TO Records(ITS.Lines)

   GET(ITS.Lines,I)

   F = ITS.ParseCSVLine(ITS.Lines.OL,False,';')

   ITS.ODS('Line: ' & ITS.Lines.OL)

   IF F

     ITS.ODS('  ' & Format(F,@n_3) & ' Fields from ' & ITS.Lines.OL)

     LOOP X = 1 TO F

       GET(ITS.CSVFields,X)

       ITS.ODS('    ' & Format(X,@n02) & ' ' & ITS.CSVFields.OL)

     END

   END

END

 

See also:

FileToLines

CSVFields

Lines



Direct link to this page: http://www.icetips.com/manuals/utilities/parsecsvline_stringclass.htm