Board index » delphi » Putting CSV items into a listbox

Putting CSV items into a listbox

I have a fields in a table which contains items separated by commas (except
the last item).  I want to put these into a list box.  I need some ideas.

ie.

Item1,Item2,Item3

needs to be

Item1
Item2
Item3

Ken

 

Re:Putting CSV items into a listbox


If what you have is something like

Field = "option1, option2, option3"

then

ListBox1.Items.CommaText := field.value

will produce a list box with the following lines
    option1
    option2
    option3

However, CommaText uses spaces or comma as a delimiter unless enclosed in
double quotes. Thus 'option 1, option 2, "option 3"' will produce
    option
    1
    option
    2
    option 3

If this is a problem, then the following should work (it won't handle
embedded commas)

procedure BreakUpOnCommas (str : string; list : tStrings);

var    i    : integer;

begin
if list <> nil
then begin
    list.clear;
    if str <> ''
    then begin
        i := pos (',', str);
        while i > 0 do
            begin
            list.add (trim (copy (str, 1, i - 1)));
            delete (str, 1, i);
            i := pos (',', str);
            end;
        // test is incase the line ends in a comma,
        // remove the test if this means an empty entry - but leave the
list.add (trim (str))
        if str <> ''
        then list.add (trim (str));
        end;
    end;
end;

Quote
Ken Pawluk <ken@NO$P@M_bjornsonconcepts.com> wrote in message

news:rufmqgrnlg711@corp.supernews.com...
Quote
> I have a fields in a table which contains items separated by commas
(except
> the last item).  I want to put these into a list box.  I need some ideas.

> ie.

> Item1,Item2,Item3

> needs to be

> Item1
> Item2
> Item3

> Ken

Re:Putting CSV items into a listbox


Wow.  That's exactly what I needed. Thanks.
Ken

Quote
Bruce Roberts <no.junk.please....@attcanada.net> wrote in message

news:3zRF3.781$SS.166@cabot.ops.attcanada.net...
Quote
> If what you have is something like

> Field = "option1, option2, option3"

> then

> ListBox1.Items.CommaText := field.value

> will produce a list box with the following lines
>     option1
>     option2
>     option3

> However, CommaText uses spaces or comma as a delimiter unless enclosed in
> double quotes. Thus 'option 1, option 2, "option 3"' will produce
>     option
>     1
>     option
>     2
>     option 3

> If this is a problem, then the following should work (it won't handle
> embedded commas)

> procedure BreakUpOnCommas (str : string; list : tStrings);

> var    i    : integer;

> begin
> if list <> nil
> then begin
>     list.clear;
>     if str <> ''
>     then begin
>         i := pos (',', str);
>         while i > 0 do
>             begin
>             list.add (trim (copy (str, 1, i - 1)));
>             delete (str, 1, i);
>             i := pos (',', str);
>             end;
>         // test is incase the line ends in a comma,
>         // remove the test if this means an empty entry - but leave the
> list.add (trim (str))
>         if str <> ''
>         then list.add (trim (str));
>         end;
>     end;
> end;

Other Threads