Ron
2006-11-01 19:34:58
Explode() Function
The explode function, that I made which was fixed by Weibs, will make an array very easily, just by doing this.
SomeArray := Explode('*', 'happy*sad*mad*angry*excited*bored');
It will then make an array with values of happy for SomeArray[0], sad for SomeArray[1], mad for SomeArray[2], etc. This is very useful if you wanted a person to provide a list of colors, and you made the separator a comma. Then in the const, you can have something like…
Const
Colors := '034294234,18473981,23423974,2384792384,23842397';
and then you could make an explode call down below like this.
ColorArray := Explode(',', Colors);
and it will do set each value to the ColorArray. :D
Explode() function code only:
// The Explode procedure looks through a string and separates it with whatever
// you suplied as a separator, and takes each value and puts them into an array.
// Example: Explode('*', 'a*b*c*d*e*');
// Made by Ron :)
function Explode(Separator, Str : string) : array of string;
var
i : integer;
begin
if(pos(Separator, Str) > 0)then
repeat
SetArrayLength(Result, i + 2);
Result[i] := copy(Str, 1, pos(Separator, Str)-1);
delete(Str, 1, pos(Result[i], Str)+Length(Result[i]));
i := i + 1;
until(pos(Separator, Str) = 0)
else
SetArrayLength(Result, 1);
Result[i] := Str;
end;
Test the Explode() function by using this program.
program TestExplode;
var
MyArray : Array of String;
i : Integer;
// The Explode procedure looks through a string and separates it with whatever
// you suplied as a separator, and takes each value and puts them into an array.
// Example: Explode('*', 'a*b*c*d*e*');
// Made by Ron :)
function Explode(Separator, Str : string) : array of string;
var
i : integer;
begin
if(pos(Separator, Str) > 0)then
repeat
SetArrayLength(Result, i + 2);
Result[i] := copy(Str, 1, pos(Separator, Str)-1);
delete(Str, 1, pos(Result[i], Str)+Length(Result[i]));
i := i + 1;
until(pos(Separator, Str) = 0)
else
SetArrayLength(Result, 1);
Result[i] := Str;
end;
begin
ClearDebug;
MyArray := Explode('*', 'ass*bitch*cunt*doosh*erm*fuck');
for i := 0 to GetArrayLength(MyArray) - 1 do
WriteLn(' MyArray[' + IntToStr(i) + '] := ''' + MyArray[i] + ''';');
end.
I hope you liked it!
~Ron :spot: