type
TArray = array of string;
function explode(cDelimiter, sValue : string; iCount : integer) : TArray;
var
s : string; i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin
inc(i);
SetLength(result, i);
p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
begin
result[i - 1] := copy(s,0,p-1);
{updated, thanks Irfan}
s := copy(s,p + length(cDelimiter),length(s));
end else
begin result[i - 1] := s;
s := ”;
end;
end;
end;
use like this
explode(‘,’,'test,test,tets’, 0) ;
or
explode(‘|’,'test|test|tets’, 2) ;
or
a := explode(‘,’,'test,test,tets’, 0) ;
for i := 0 to High(a) do
begin
showMessage(a[i]);
end;
Hi Jason,
Nice function, just a piece of advice, like you do in D-TNT, can you put a call example in when you write a function or procedure? It’s just helpful sometimes….. it’s an idea
Si
Hello,
there is a little problem in your code…
Doesnt work with Length(delimiter )>0…
Here is updated code for lengthy delimiters…
function explode(cDelimiter, sValue : string; iCount : integer) : TArray;
var
s : string; i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin
inc(i);
SetLength(result, i);
p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i
function explode(cDelimiter, sValue : string; iCount : integer) : TArray;
var s:string; i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin inc(i); SetLength(result, i); p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i
update after IF statement:
begin
result[i - 1] := copy(s,0,p-1); s := copy(s,p + length(cDelimiter),length(s));
end
Thanks Irfan for your comment, I’ll have a look at your suggestion and possibly update the page.
Kindest Regards
Jason
nice functions… it is very usable in a lot of applications… tq
thank choirul, glad you like it.
i was missing this function in pascal/delphi and i didnt feel like writing it.
thank You for this!
hi vac, glad it was helpful
You might like this:
procedure Whatever;
var myArray: TStringList;
begin
myArray := TStringList.Create;
myArray.Delimiter := ‘,’; { or whatever you want }
myArray.DelimitedText := ‘one,two,three’;
end;
and that’s it. It’s a shorter solution but with one major drawback, the delimiter can only be one char.
Works like a charm..
Very big thank’s for function
Cool function, thanks.