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;

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • MisterWong
  • Reddit
  • Technorati

11 Responses to “Delphi explode() function like PHP explode()”


  1. 1 Simon J Stuart

    Hi Jason,
    Nice function, just a piece of advice, like you do in D-TNT, can you put a call example in when you write a function or procedure? It’s just helpful sometimes….. it’s an idea :)

    Si

  2. 2 Irfan

    Hello,

    there is a little problem in your code…

    Doesnt work with Length(delimiter )>0…

    Here is updated code for lengthy delimiters…

    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

    :)

  3. 3 Irfan

    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

  4. 4 Irfan

    update after IF statement:

    begin
    result[i - 1] := copy(s,0,p-1); s := copy(s,p + length(cDelimiter),length(s));
    end

  5. 5 jason.white

    Thanks Irfan for your comment, I’ll have a look at your suggestion and possibly update the page. :)

    Kindest Regards

    Jason

  6. 6 choirul ihwan

    nice functions… it is very usable in a lot of applications… tq :)

  7. 7 jason.white

    thank choirul, glad you like it.

  8. 8 vac

    i was missing this function in pascal/delphi and i didnt feel like writing it.
    thank You for this!

  9. 9 jason.white

    hi vac, glad it was helpful :)

  10. 10 Jak Spalding

    You might like this:

    procedure Whatever;
    var myArray: TStringList;
    begin
    myArray := TStringList.Create;
    myArray.Delimiter := ‘,’; { or whatever you want }
    myArray.DelimitedText := ‘one,two,three’;
    end;

    and that’s it. It’s a shorter solution but with one major drawback, the delimiter can only be one char.

  11. 11 Technology Blog

    Works like a charm..

Leave a Reply