Find the commas!

Hi,
I have a sting like:
word1,word2,word3

It is all in a string variable called "curr", how do I put the different words in different string variables? The words are separated by commas.
MAXcomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi maxcom,

you can use a tsringlist like

var sl : TstringList;
begin
  sl := TstringList.Create;
  try
    sl.commatext := curr;
    //now you can access the words by index like
    myvar1 := sl[0];
    myVar2 := sl[1];  //...
  finally
    sl.free
  end;
end;

meikl
0
_DJ_Commented:

in this example I use a listbox, you can also use a stringlist or whatever you wish.
I don't sit in front of delphi right now but I'll try to answer you question anyway. You might have to check it in the compiler .


var
I: integer;
begin
 while pos(',',myvar1)>0 do begin
 listbox1.items.add(copy(myvar1,0,pos(',',myvar1));
 myvar1 := copy(myvar,pos(',',myvar1),length(myvar1)-pos(',',myvar1));
 end;
end;

I guess that should be it, but as I said I haven't tried to compile it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_DJ_Commented:
oups, there was no use to declare the varialbe I :)
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

kretzschmarCommented:
hmm,

by the way of using a tlistbox u can use
listbox1.items.commatext := curr;

;-)
0
QuaLjynCommented:
Maxcom, if I were you, I would buy Kretzmars solution *smiling* I wish i could give you a better one, but I think this one is the most userfriendly around :-)

  -Kim
0
_DJ_Commented:
Hey!

ok, now I got home, I can see that I made a few errors in the code :)

I have corrected these and here are some code that works!

I made an Editbox, a listbox and a button in this example and here is the onclickevent on the button:

procedure TForm1.Button1Click(Sender: TObject);
var
myvar1: string;
begin
myvar1:=edit1.text;
 while pos(',',myvar1)>0 do begin
 listbox1.items.add(copy(myvar1,0,pos(',',myvar1)-1));
 myvar1 := copy(myvar1,pos(',',myvar1)+1,length(myvar1));
 if pos(',',myvar1)=0 then
 listbox1.items.add(myvar1);
 end;
end;

That will work :)

QuaLjyn >> Who said code had to be userfriendly ??
0
kretzschmarCommented:
well dj,

because we are about to left the vcl-features beside,
what about this (same configuration as yours)

//Function for split strings
function SplitIt(Splitter : String;
                 Var ToSplit : String) : String;
Var P : Integer;
begin
  P := Pos(Splitter, ToSplit);
  If P = 0 then P := Length(ToSplit)+1;
  Result := Copy(ToSplit,1,p-1);
  ToSplit := Copy(ToSplit,p+length(splitter),MaxLongInt);
end;

//Example for use
procedure TForm1.Button1Click(Sender: TObject);
var
  S : String;
begin
  S := Edit1.Text;
  while S <> '' do Listbox1.Items.Add(SplitIt(',',s));
end;

meikl ;-)
0
MAXcomAuthor Commented:
thanks!
0
_DJ_Commented:
kretzschmar:
yearh, your right, I should have made it a function :)
0
kretzschmarCommented:
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.