Archive for the 'Functions' Category

Delphi explode() function like PHP explode()

type
    TArray = array of string;

function explode(cDelimiter,  sValue : string; iCount : integer) : TArray;
var
s : string; i,p : integer;
begin

        s := sValue; i := 0;
        while length(s) > 0 do
        begin
                inc(i);
                SetLength(result, i);
                p := pos(cDelimiter,s);

                if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
                begin

                        
                        result[i - 1] := copy(s,0,p-1);

                        {updated, thanks Irfan}
                        s := copy(s,p + length(cDelimiter),length(s));
                end else
                begin result[i - 1] := s;
                        s :=  ”;
                end;
        end;

end;

use like this

explode(‘,’,'test,test,tets’, 0) ;
or
explode(‘|’,'test|test|tets’, 2) ;
or

a := explode(‘,’,'test,test,tets’, 0) ;
for i := 0 to High(a) do
begin
showMessage(a[i]);
end;