Skip to content →

Membuat fungsi “explode” di Pascal

Untuk memecah variabel string berdasar delimiter ke dalam variabel array kita dapat melakukanya dengan mudah di PHP. tetapi di Pascal, kita harus mengutak atik sendiri.
misalkan
variable a=1,3,5,2,4
kalo di explode tanda koma pake (,) akan menjadi :
b[0] = 1
b[1] = 2
b[2] = 3
b[3] = 4
b[4] = 5

berikut ini fungsi explode di Pascal
[pascal]
function Explode(const str: string; const separator: string): TStrings;
var
n: integer;
p, q, s: PChar;
item: string;
begin
Result := TStringList.Create;
try
p := PChar(str);
s := PChar(separator);
n := Length(separator);
repeat
q := StrPos(p, s);
if q = nil then q := StrScan(p, #0);
SetString(item, p, q – p);
Result.Add(item);
p := q + n;
until q^ = #0;
except
item := ”;
Result.Free;
raise;
end;
end;
[/pascal]

Published in Coding Dunia IT Pascal - Delphi

2 Comments

    • hangga hangga

      terimakasih, atas kunjungannya..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.