unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
SrsList,DestList1,DestList2: TStringList;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
SrsList:=TStringList.Create;
SrsList.Add('a');
SrsList.Add('b');
SrsList.Add('*');
SrsList.Add('c');
SrsList.Add('d');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SrsList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
begin
DestList1:=Tstringlist.Create;
DestList2:=TStringlist.Create;
try
i:=SrsList.IndexOf('*');
for j:=0 to i-1 do
DestList1.Add(SrsList.Strings[j]);
for j:=i+1 to SrsList.Count-1 do
DestList2.Add(SrsList.Strings[j]);
Memo1.Lines.AddStrings(DestList1);
Memo2.Lines.AddStrings(DestList2);
finally
DestList1.Free;
DestList2.Free;
end;
end;
end.
//根据字符串,拆分字符串,相当于vb中的split函数
function SplitString(const Source,ch:string):TStringList;
var
temp:String;
i:Integer;
begin
Result:=TStringList.Create;
//如果是空自符串则返回空列表
if Source=''
then exit;
temp:=Source;
i:=pos(ch,Source);
while i<>0 do
begin
Result.add(copy(temp,0,i-1));
Delete(temp,1,i);
i:=pos(ch,temp);
end;
Result.add(temp);
end;
用这个的SplitString吧
sStrList .Delimiter := '*';
sStrList .DelimitedText :=sStrList.Text;
alist:=Tstringlist.create;
blist:=Tstringlist.create;
clist:=Tstringlist.create;//blist,clist是分割后的子集
for i:=0 to cnt do
begin
blist.add(alist[i]);
end;
for j:=cnt+1 to alist.count do
begin
clist.add(alist[j]);
end;
分割后两个子list大小为1-cnt 和cnt-alist。count