ItemExtractCsv

Returns the selected item from a CSV line.

Syntax:

ItemExtractCsv (index, line, flags [, delimiter])

Parameters:

(i) index the ordinal position of the item to be returned.

(s) line a CSV line (see below).

(i) flags can be set to 0, or can specify one or more of the following values combined with the bitwise OR ('|') operator:
Value
 Meaning
1 treat leading and trailing whitespace as significant

(s) delimiter (optional) specifies the character used to separate values in the line. It can be any single character except a space, null, carriage return, line feed or quotation mark (double quote). If omitted, a comma will be used as the delimiter.

Returns:

(s) the selected item.

 

This function is like ItemExtract, but instead of a list, it extracts items from a CSV line. A CSV ("comma separated values") line consists of comma-delimited items. Items may optionally be enclosed in quotation marks (aka, "double quotes").

There are two cases where an item *must* be enclosed in quotation marks:
(1) If the item contains a comma
(2) If the item contains a quotation mark

Here are some examples:

 

1,2,3,4,5

"January","February","March"

"Doe, John",20

 

In order to embed a quotation mark in an item, you must replace it with two quotation marks (i.e., add an extra quotation mark). For example, you would specify the item "My Fair Lady" (with quotes) as:

 

"""My Fair Lady"""

 

Each quotation mark is doubled, and the entire item is enclosed in a set of quotation marks.

Normally, whitespace outside of quoted strings is ignored. The following two lines would be considered identical:

 

A,B,C

A, B, C

 

But if you specify a "flags" value of 1, then the whitespace will be considered part of the items. Whitespace *inside* a quoted string is always preserved, regardless of the "flags" value:

 

" This line has spaces around it "

 

Similarly, whitespace outside of a quoted string is always ignored. i.e., the following two lines are considered identical, regardless of the "flags" value

 

"String 1","String 2","String 3"

"String 1", "String 2", "String 3"

 

The function returns the selected item, with any enclosing quotes removed, and any doubled quotation marks converted to normal quotation marks. Leading and trailing whitespace may be stripped out, as described above.

 

Example:


line = '"January","February","March"'
count = ItemCountCSV(line, 0,",")
For x = 1 To count
   item = ItemExtractCSV(x,line,0,",")
   Message(StrCat("Item number ",x),item)
Next
See Also:

ItemCount, ItemCountCsv, ItemExtract, ItemInsert, ItemLocate, ItemLocateWild, ItemRemove, ItemReplace, ItemSort, Lists Explained