Link to home
Start Free TrialLog in
Avatar of MAXcom
MAXcom

asked on

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.
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
ASKER CERTIFIED SOLUTION
Avatar of _DJ_
_DJ_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _DJ_
_DJ_

oups, there was no use to declare the varialbe I :)
hmm,

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

;-)
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
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 ??
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 ;-)
Avatar of MAXcom

ASKER

thanks!
kretzschmar:
yearh, your right, I should have made it a function :)