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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
;-)
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
-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(my var1,0,pos (',',myvar 1)-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 ??
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
var
myvar1: string;
begin
myvar1:=edit1.text;
while pos(',',myvar1)>0 do begin
listbox1.items.add(copy(my
myvar1 := copy(myvar1,pos(',',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(spli tter),MaxL ongInt);
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 ;-)
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(spli
end;
//Example for use
procedure TForm1.Button1Click(Sender
var
S : String;
begin
S := Edit1.Text;
while S <> '' do Listbox1.Items.Add(SplitIt
end;
meikl ;-)
ASKER
thanks!
kretzschmar:
yearh, your right, I should have made it a function :)
yearh, your right, I should have made it a function :)
;-)
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