SRL Archive - Research & Development Lounge

ImplodeFrom and RevStrArr


mixster

2009-08-07 18:30:35

ImplodeFrom - like a regular Implode or srl_Implode except that you specify the range to implode. Handy when you want to leave out the beginning but don't want to go through and swap all the indexes around then set the length to one less etc.

function ImplodeFrom(input: TStringArray; del: string; s, e: Integer): string;
var
h, i: Integer;
begin
Result := '';
h := Length(input) - 1;
if (h = -1) then
exit;

if (s > e) then
Swap(s, e);

s := Max(s, 0);
e := Max(e, 0);

s := Min(s, h);
e := Min(e, h);

Result := input[s];

if (s = e) then
exit;

for i := s + 1 to e do
Result := Result + del + input[i];
end;


RevStrArr - Reverses the string array, ie swap highest with lowest, second highest with second lowest etc. Useful for when you need to process a string array as you can just the length for one lower and loop from high to 0.
procedure RevStrArr(var arr: TStringArray);
var
h, i, m: Integer;
begin
h := Length(arr);
m := Trunc(h / 2.0) - 1;
h := h - 1;

for i := 0 to m do
Swap(arr[i], arr[h - i]);
end;

Simtoon

2009-08-08 03:14:38

Nice work mixster <3