Discussion:
Explode function not working properly
(too old to reply)
Vlastimil Burian
2015-02-19 12:34:52 UTC
Permalink
type
TDynamicStringArray = array of string;


function Explode(const Separator: string; S: string): TDynamicStringArray;

var
I, SepPos, SepLen: Integer;

begin
if not (Separator <> '')
then raise EInvalidInputStringFunctions.Create('Explode');

I := 0;
SetLength(Result, 1);

SepLen := Length(Separator);
SepPos := Pos(Separator, S);
while SepPos > 0 do begin
Result[I] := Copy(S, 1, SepPos - 1);
Delete(S, 1, SepPos + SepLen - 1);
SepPos := Pos(Separator, S);
Inc(I);
SetLength(Result, I + 1);
end;

Result[I] := S;
end;


What is wrong? Please help.
Vlastimil Burian
2015-02-20 09:26:46 UTC
Permalink
Post by Vlastimil Burian
type
TDynamicStringArray = array of string;
function Explode(const Separator: string; S: string): TDynamicStringArray;
var
I, SepPos, SepLen: Integer;
begin
if not (Separator <> '')
then raise EInvalidInputStringFunctions.Create('Explode');
I := 0;
SetLength(Result, 1);
SepLen := Length(Separator);
SepPos := Pos(Separator, S);
while SepPos > 0 do begin
Result[I] := Copy(S, 1, SepPos - 1);
Delete(S, 1, SepPos + SepLen - 1);
SepPos := Pos(Separator, S);
Inc(I);
SetLength(Result, I + 1);
end;
Result[I] := S;
end;
What is wrong? Please help.
Fixed.

Loading...