var i,j: Integer; P: array [1..11] of integer; TP: String; begin j := 1; for i := 1 to length(S) do begin if S[i] <> ';' then TP := TP + S[i]; else begin P[j] := StrToInt(TP); TP := '; end; end; end;
大概就是下面的意思,可能有手误,你自己调一下 //变量 p:array of integer; i,idx:Integer;//代码 idx=0; while not i do begin i:=Pos(';',s); Inc(idx); SetLength(p,idx); p[idx-1]:=StrToInt(Copy(s,1,idx-1)); Delete(s,1,idx); end;
var s,x:string; i:integer; begin s:='1;2;3;4;5;6;7;8;9;10;11'; i:=pos(';',s); x:=leftstr(s,i-1); showmessage(x); delete(s,1,i); showmessage(s); end; 思路就是这样的!!
//变量 p:array of integer; i,idx:Integer;//代码 idx=0; while i do //不好意思,这里改一下 begin i:=Pos(';',s); Inc(idx); SetLength(p,idx); p[idx-1]:=StrToInt(Copy(s,1,idx-1)); Delete(s,1,idx); end;
var strlist:TStrings i:integer; p:array of integer; begin strlist:=TStringList.Create; strlist.delimiter=';'; strlist.delimitertext:=s; //s:='1;2;3;4;5;6;7;8;9;' Setlength(p,strlist.count); for i:=0 to strlist.count-1 do p[i]:=StrToInt(strlist[i]); strlist.free; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TIntArray=array of Integer; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} function SplitStrToInt(Str1,Str2:String;var NumArray:TIntArray):Integer; var p,lp,l1,l2:Integer; subs:String; begin l1:=Length(Str1); l2:=Length(Str2); p:=1; SetLength(NumArray,l1 div 2); Result:=0; repeat lp:=Pos(Str2,Str1); subs:=Copy(Str1,p,lp-1); Str1:=Copy(Str1,lp+l2,l1-lp-l2+1); l1:=Length(Str1); if(trim(subs)<>'')then begin NumArray[Result]:=StrToInt(subs); Inc(Result); end; until l1=0; end; procedure TForm1.Button1Click(Sender: TObject); var a:TIntArray; l,i:Integer; begin l:=SplitStrToInt('12;322;12422;100;',';',a); for i:=0 to l-1 do ShowMessage(IntToStr(a[i])); l:=SplitStrToInt('12@@@322@@@12422@@@100@@@','@@@',a); for i:=0 to l-1 do ShowMessage(IntToStr(a[i])); end;end.
ICMGDCHN(60 DAYS->喜欢明月) 是正解,比较方便
function PartitionString(StrV,PrtSymbol: string): TStringList; var iTemp: integer; begin result := TStringList.Create; iTemp := pos(PrtSymbol,StrV); while iTemp>0 do begin if iTemp>1 then result.Append(copy(StrV,1,iTemp-1)); delete(StrV,1,iTemp+length(PrtSymbol)-1); iTemp := pos(PrtSymbol,StrV); end; if Strv<>'' then result.Append(StrV); end;调用的时候这样: var Str:TStrings begin Str:=PartitionString('1;2;3;4;5;6;7;8;9;10;11',';'); end;Str就是你要的了。
P: array [1..11] of integer;
TP: String;
begin
j := 1;
for i := 1 to length(S) do
begin
if S[i] <> ';' then
TP := TP + S[i];
else
begin
P[j] := StrToInt(TP);
TP := ';
end;
end;
end;
//变量
p:array of integer;
i,idx:Integer;//代码
idx=0;
while not i do
begin
i:=Pos(';',s);
Inc(idx);
SetLength(p,idx);
p[idx-1]:=StrToInt(Copy(s,1,idx-1));
Delete(s,1,idx);
end;
i:integer;
begin
s:='1;2;3;4;5;6;7;8;9;10;11';
i:=pos(';',s);
x:=leftstr(s,i-1);
showmessage(x);
delete(s,1,i);
showmessage(s);
end;
思路就是这样的!!
p:array of integer;
i,idx:Integer;//代码
idx=0;
while i do //不好意思,这里改一下
begin
i:=Pos(';',s);
Inc(idx);
SetLength(p,idx);
p[idx-1]:=StrToInt(Copy(s,1,idx-1));
Delete(s,1,idx);
end;
strlist:TStrings
i:integer;
p:array of integer;
begin
strlist:=TStringList.Create;
strlist.delimiter=';';
strlist.delimitertext:=s; //s:='1;2;3;4;5;6;7;8;9;'
Setlength(p,strlist.count);
for i:=0 to strlist.count-1 do
p[i]:=StrToInt(strlist[i]);
strlist.free;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TIntArray=array of Integer;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function SplitStrToInt(Str1,Str2:String;var NumArray:TIntArray):Integer;
var
p,lp,l1,l2:Integer;
subs:String;
begin
l1:=Length(Str1);
l2:=Length(Str2);
p:=1;
SetLength(NumArray,l1 div 2);
Result:=0;
repeat
lp:=Pos(Str2,Str1);
subs:=Copy(Str1,p,lp-1);
Str1:=Copy(Str1,lp+l2,l1-lp-l2+1);
l1:=Length(Str1);
if(trim(subs)<>'')then
begin
NumArray[Result]:=StrToInt(subs);
Inc(Result);
end;
until l1=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a:TIntArray;
l,i:Integer;
begin
l:=SplitStrToInt('12;322;12422;100;',';',a);
for i:=0 to l-1 do
ShowMessage(IntToStr(a[i]));
l:=SplitStrToInt('12@@@322@@@12422@@@100@@@','@@@',a);
for i:=0 to l-1 do
ShowMessage(IntToStr(a[i]));
end;end.
var
iTemp: integer;
begin
result := TStringList.Create;
iTemp := pos(PrtSymbol,StrV);
while iTemp>0 do begin
if iTemp>1 then result.Append(copy(StrV,1,iTemp-1));
delete(StrV,1,iTemp+length(PrtSymbol)-1);
iTemp := pos(PrtSymbol,StrV);
end;
if Strv<>'' then result.Append(StrV);
end;调用的时候这样:
var
Str:TStrings
begin
Str:=PartitionString('1;2;3;4;5;6;7;8;9;10;11',';');
end;Str就是你要的了。