Explode function for Delphi/Lazarus

14th Jun 2005Archyvas, English, Archyvas, Kita, Archyvas, Kita, Senienos

PHP has a nice function Explode, that divides string into several smaller strings using some symbol as a divider: for example string ‘Cats|Dogs|Cows|Horses’ can be exploded to array(’Cats’,’Dogs’,’Cows’,’Horses’);
Today I wrote a similar function for my Lazarus project. If someone needs it:


function Explode(S, Bomb: String): TStrings;
var i : integer;
p : string;
begin
result := TstringList.Create;
while Pos(Bomb,s)<> 0
do begin
p := ‘’;
for i := 1 to Pos(Bomb,s)-1
do p := p + s[i];
result.Add(p);
Delete(s,1,Pos(Bomb,s));
end;
result.Add(s);
end;

1 Comment Comments Feed

  1. Maloupi (2011-09-14, 17:35).

    Thank you !
    I modified the delete ligne so it can work with bomb longer than 1 character :
    Delete(s,1,Pos(Bomb,s) + Length(Bomb)-1);

Add a Comment