|
| PHP Alike implode & explode functions for Delphi |  
|
|---|
| 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: 6 | Visits: 6600 | 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;
|
|
|
| |
Sign up to consume product discounts for Bronze memberships !
|
|
| |
Community Ad of D. Wischnewski |
|
| |
|
|
|