I prefer a helper with no dependency on StrUtils.
Ah, indeed. i was being lazy
As a matter of fact so lazy that i don't even wanted to use a type helper
program arraymore;
{$MODE OBJFPC}
{.$DEFINE USETYPEHELPERS}
{$IFDEF USETYPEHELPERS}
{$MODESWITCH TYPEHELPERS}
{$ENDIF}
Uses
Heaptrc, StrUtils;
type
TStringDynArray = array of ansistring;
{$IFDEF USETYPEHELPERS}
type
TStringDynArrayHelper = type helper for TStringDynArray
class function CreateFromDelimitedString(DelimString: String): TStringDynArray static;
end;
class function TStringDynArrayHelper.CreateFromDelimitedString(DelimString: String): TStringDynArray;
var
num, i : Integer;
begin
num := WordCount(DelimString, [',']);
SetLength(Result, num);
for i := 1 to num do
begin
Result[i-1] := ExtractWord(i, DelimString, [',']);
end;
end;
operator := (S : string): TStringDynArray;
begin
Result := TStringDynArray.CreateFromDelimitedString(S);
end;
{$ELSE}
operator := (S : string): TStringDynArray;
var
num, i : Integer;
begin
num := WordCount(S, [',']);
SetLength(Result, num);
for i := 1 to num do
begin
Result[i-1] := ExtractWord(i, S, [',']);
end;
end;
{$ENDIF}
const
NumberStr = 'zero,one,two,three,four,five,six,seven';
var
nums : TStringDynArray;
idx : integer = 0;
s : ansistring;
begin
// initialize array
// nums:=TStringDynArray.create(NumberStr);
// nums:=TStringDynArray.CreateFromDelimitedString(NumberStr);
nums := NumberStr;
// display array value(s)
for s in nums do begin
WriteLn('Index:',idx,' ',s);
Inc(idx);
end;
end.
Mind-boggling in case you use these kind of things to much/deep.
Edit:
Hmz, seems i misunderstood something ?:
const
NumberStr = 'zero,one,two,three,four,five,six,seven';
var
nums : TStringDynArray = NumberStr;
produces:
Fatal: Syntax error, "NIL" expected but "identifier NUMBERSTR" found
Is that not considered an assignment ? In which case it should be covered by the explicit assignment operator ? If not the case, is there a separate operator for such assignments ? (i was unable to find in the docs)