Explode TArray

function Explode(const Separator, S: string; Limit: Integer = 0): TArray;
var
SepLen : Integer;
F, P : PChar;
ALen, Index : Integer;
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);
ALen := Limit;
SetLength(Result, ALen);

Index := 0;
P := PChar(S);
while P^ <> #0 do
begin
F := P;
P := StrPos(P, PChar(Separator));
if (P = nil) or ((Limit > 0) and (Index = Limit - 1)) then
P := StrEnd(F);
if Index >= ALen then
begin
Inc(ALen, 5); //
SetLength(Result, ALen);
end;
SetString(Result[Index], F, P - F);
Inc(Index);
if P^ <> #0 then
Inc(P, SepLen);
end;
if Index < ALen then
SetLength(Result, Index); //
end;

type
TArray = array of string;

.....

var
arr: TArray;
begin
arr := explode(',', 'Mark,Michel,segment,');
end;

上一篇:在Win7虚拟机下搭建Hadoop2.6.0伪分布式环境


下一篇:摘要: CentOS 6.5搭建Redis3.2.8伪分布式集群