Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


PHP Alike implode & explode functions for DelphiGo to Ronald Buster's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Implode & Explode
Product:
Delphi 6.x (or higher)
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
03/28/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet delphi implode explode php string array
Times Scored:
5
Visits:
4767
Uploader: Ronald Buster
Company: DISPI software Engineering
Reference: http://www.dispi.com
 
Question/Problem/Abstract:
Implode & Explode routines just for Dephi making life easy ...
Answer:



//////////////////////////////////////////////////////////////////////////
// Procedure - implode
// Author    - Ronald Buster
// Remarc    - PHP like implode function
//
// Returns a string containing a string representation of all the array
// elements in the same order, with the glue string between each element.
//////////////////////////////////////////////////////////////////////////

function implode(const glue: string; const pieces: array of string): string;
var I: Integer;
begin
  Result := '';
  for I := 0 to High(Pieces) do
    Result := Result + Glue + Pieces[I];
  Delete(Result, 1, Length(Glue));
end;

//////////////////////////////////////////////////////////////////////////
// Procedure - explode
// Author    - Ronald Buster
// Remarc    - PHP like explode function
//
// Returns an array of strings, each of which is a substring of string
// formed by splitting it on boundaries formed by the string separator.
// If limit is set, the returned array will contain a maximum of limit
// elements with the last element containing the rest of string.
//
//////////////////////////////////////////////////////////////////////////

function explode(const separator, s: string; limit: Integer = 0): TDynStringArray;
var SepLen: Integer;
    F, P: PChar;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = '' then
    begin
      SetLength(Result, 1);
      Result[0] := S;
      Exit;
    end;
  SepLen := Length(Separator);

  P := PChar(S);
  while P^ <> #0 do
    begin
      F := P;
      P := AnsiStrPos(P, PChar(Separator));
      if (P = nil) or ((Limit > 0) and (Length(Result) = Limit - 1)) then
        P := StrEnd(F);
      SetLength(Result, Length(Result) + 1);
      SetString(Result[High(Result)], F, P - F);
      F := P;
      while (P^ <> #0) and (P - F < SepLen) do
        Inc(P);
    end;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Excellent idea
    Sam Rauch (May 8 2003 1:04PM)

I fell in love with PHP arrays shortly after I started using them!  I'm not familiar with many other languages, but I wonder from time to time if Delphi's array types are a little lacking in functionality compared to their peers.  

To make these routines truly exceptional, you could expand them to take any type of array and return the contents as a string.  I'm not saying I know how to do it, but I would be interested in what you come up with!  
Respond

RE: Excellent idea
Ronald Buster (May 8 2003 3:05PM)

So you would like to convert any type of array to a string type array.

Let me see what I can do ok. I will be back on this issue shortly.

Regards,

Ronald
Respond

RE: RE: Excellent idea
Ronald Buster (Feb 18 2004 11:43AM)

look at :

http://www.delphi3000.com/articles/article_3909.asp
Respond

RE: Excellent idea
Borland Delphi (May 31 2006 6:40AM)

I have fixed above function. It does not work correctly in case:
ParamStrings := Explode(':', 'test:another:');
it should return array('test', 'another', '') so 3 elements but it return only 2!!! So it cat last one in case if passed string finished with separtor.
In most cases it is very important, that you will know how many elements you will have in array!!!
p.s. Explode('/', '/') should return 2 empty strings, before and after '/'.

function Explode(const Separator, S: String; Limit: Integer = 0): TDynStringArray;
  var
    SepLen: Integer;
    F, P: PChar;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = '' then
    begin
      SetLength(Result, 1);
      Result[0] := S;
      Exit;
    end;
  SepLen := Length(Separator);

  P := PChar(S);
  while P^ <> #0 do
    begin
      F := P;
      P := AnsiStrPos(P, PChar(Separator));
      if (P = nil) or ((Limit > 0) and (Length(Result) = Limit - 1)) then
        P := StrEnd(F);
      SetLength(Result, Length(Result) + 1);
      SetString(Result[High(Result)], F, P - F);
      F := P;
      //ERROR in case if string is finished with separator
      if P = Separator then
        SetLength(Result, Length(Result) + 1);
      //END of modification
      while (P^ <> #0) and (P - F < SepLen) do
        Inc(P);
    end;
end;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)