How to explode some string in delphi
In delphi, you can make a function explode as in php, copy the code below to do that :
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); end; end;
How to use it?
To use function above you can call it from button click as example below :
procedure TForm1.Button1Click(Sender: TObject); var ls:string; i:integer; arr:TDynStringArray; begin ls := 'apple;banana;mango;orange;melon'; arr := explode(';', ls); for i := 0 to Length(arr)-1 do ShowMessage(arr[i]); end;