Implode / Explode methods like in PHP (Views: 29)
Problem/Question/Abstract: In Delphi you can also use the implode and explode methods from PHP: Answer: type TDynStringArray = array of string; 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; 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); // nächsten Anfang ermitteln end; end; |