[ACCEPTED]-How to split a string in Inno Setup-pascalscript

Accepted answer
Score: 16

For anyone who prefers the function format, I 1 have modified @cezarlamann's answer:

function StrSplit(Text: String; Separator: String): TArrayOfString;
var
  i, p: Integer;
  Dest: TArrayOfString; 
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := Copy(Text, 1, p-1);
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := Text;
      Text := '';
    end;
  until Length(Text)=0;
  Result := Dest
end;
Score: 13

I've been looking for the same thing today...

This 6 one works just fine on Inno Setup Scripts. Paste 5 this excerpt inside your script before the 4 procedure/function which will call this 3 "split" procedure.

You can also 2 modify this onto a function, if you desire...

procedure Explode(var Dest: TArrayOfString; Text: String; Separator: String);
var
  i, p: Integer;
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := Copy(Text, 1, p-1);
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := Text;
      Text := '';
    end;
  until Length(Text)=0;
end;

procedure Whatever();
var 
  str: String;
  strArray: TArrayOfString;
  i: Integer;
begin
  Explode(strArray,str,'.');
  for i:=0 to GetArrayLength(strArray)-1 do begin
    { do something }
  end;
end;

Taken 1 from here

Score: 3

Here's what I use:

procedure SplitString(S, Delim: string; var Dest: TArrayOfString);
var
  Temp: string;
  I, P: Integer;
begin
  Temp := S;
  I := StringChangeEx(Temp, Delim, '', true);
  SetArrayLength(Dest, I + 1);
  for I := 0 to GetArrayLength(Dest) - 1 do
  begin
    P := Pos(Delim, S);
    if P > 0 then
    begin
      Dest[I] := Copy(S, 1, P - 1);
      Delete(S, 1, P + Length(Delim) - 1);
    end
    else
      Dest[I] := S;
  end;
end;

This version avoids repeated 8 array resizing by counting the delimiters 7 using StringChangeEx and setting the array size only once. Since 6 we then know the array size, we can just 5 use a for loop. I also opted for Delete rather than 4 Copy, which (IMO) makes the code easier to read. (This 3 version also fixes the bug where the split 2 does not occur correctly if the delimiter 1 is longer than 1 character.)

Score: 1

If there is a possibility that the delimiter 3 could also be right at the end of the string, then 2 this is what I used (modified from @timyha's 1 answer)

function StrSplit(Text: String; Separator: String): TArrayOfString;
var
  i, p: Integer;
  Dest: TArrayOfString; 
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);

    if p > 0 then begin
      Dest[i] := Copy(Text, 1, p-1);
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
      
      //add an empty string if delim was at the end 
         
      if Text = '' then begin
        Dest[i]:=''; 
        i := i + 1;
      end;

    end else begin
      Dest[i] := Text;
      Text := '';
    end;
  until Length(Text)=0;
  Result := Dest
end;

More Related questions