function ConnectionStringToList(ConnectionString:string):TStringList; var ResultList:TStringList; Str:string;begin ResultList:=TStringList.Create; ResultList.Clear; Str:=ConnectionString; while Pos(';',Str)>0 do begin ResultList.Add(Copy(Str,1,Pos(';',Str)-1)); Str:=Copy(Str,Pos(';',Str)+1,Length(Str)-Pos(';',Str)+1); end; ResultList.Add(Str); Result:=ResultList; end;
TO: heartstill 你用的Delphi版本是多少?我用Delphi 6是可以的
Edit1为要分割的串,子串存在ListBox中 procedure TForm1.Button1Click(Sender: TObject); var P:Integer; begin P:=Pos(';',Edit1.Text); while P<>0 do begin ListBox1.Items.Add(Copy(Edit1.Text,1,P-1)); Edit1.Text:=Copy(Edit1.Text,P+1,Length(Edit1.Text)-P); P:=Pos(';',Edit1.Text); end; if Edit1.Text<>'' then ListBox1.Items.Add(Edit1.Text); end;
///分割一个字符串,其中分割的标志是ch 用法: var str:tstrings; str:=SplitString(source,';'); function SplitString(const source,ch:string):tstrings;function SplitString(const source,ch:string):tstrings; var temp:string; i:integer; begin result:=tstringlist.Create; 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;
var t :array of string; //数组
r: string; //要分割的字符串
i: integer;begin
i:=0;
repeat
t[i]:=copy(r,1,pos(';',r));
r:= copy(r,pos(';',r)+1,length(r)-pos(';',r));
until pos(';',r)=0;end;
const
s = 'abc;defg;higk;112213;';
var
StrList: TStringList;
begin
StrList := TStringList.Create;
try
StrList.Delimiter := ';';
// 字符串已经以;为分割符分开
StrList.DelimitedText := s;
// 验证
Memo1.Lines.Assign(StrList);
finally
FreeAndNil(StrList;);
end;
end;
FreeAndNil(StrList;);
应该是
FreeAndNil(StrList);
var
ResultList:TStringList;
Str:string;begin
ResultList:=TStringList.Create;
ResultList.Clear;
Str:=ConnectionString;
while Pos(';',Str)>0 do begin
ResultList.Add(Copy(Str,1,Pos(';',Str)-1));
Str:=Copy(Str,Pos(';',Str)+1,Length(Str)-Pos(';',Str)+1);
end;
ResultList.Add(Str);
Result:=ResultList;
end;
你用的Delphi版本是多少?我用Delphi 6是可以的
procedure TForm1.Button1Click(Sender: TObject);
var
P:Integer;
begin
P:=Pos(';',Edit1.Text);
while P<>0 do
begin
ListBox1.Items.Add(Copy(Edit1.Text,1,P-1));
Edit1.Text:=Copy(Edit1.Text,P+1,Length(Edit1.Text)-P);
P:=Pos(';',Edit1.Text);
end;
if Edit1.Text<>'' then
ListBox1.Items.Add(Edit1.Text);
end;
用法:
var str:tstrings;
str:=SplitString(source,';');
function SplitString(const source,ch:string):tstrings;function SplitString(const source,ch:string):tstrings;
var
temp:string;
i:integer;
begin
result:=tstringlist.Create;
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;