你要请出传说中的 ExtractStrings进行字符串分割 var temp:TStrings; nums:array of integer; i:integer; begin temp:=TStringlist.Create; ExtractStrings([' '],[],PChar(edit1.text),temp); for i:=0 to temp.count - 1 do SetLength(nums,temp.count); begin nums[i]:=StrToInt(temp.strings[i]); end; end;
上面的代码位置错了 看这里 var temp:TStrings; nums:array of integer; i:integer; begin temp:=TStringlist.Create; ExtractStrings([' '],[],PChar(edit1.text),temp); SetLength(nums,temp.count); for i:=0 to temp.count - 1 do begin nums[i]:=StrToInt(temp.strings[i]); end; end;
procedure TForm1.Button1Click(Sender: TObject); var I: Integer; A: array of Integer; S0, S1: string; begin S0 := trim(Edit1.text) + ' '; S1 := ''; SetLength(A, 0); for I := 1 to Length(S0) do if S0[I] <> ' ' then S1 := S1 + S0[I] else begin SetLength(A, Length(A) + 1); A[High(A)] := StrToIntDef(S1, 0); S1 := ''; end; end;
回复1楼:如果要用到TStringlist 那么直接照下面这样就行啦,为什么还要ExtractStrings([' '],[],PChar(edit1.text),temp);这么麻烦??temp := TStringlist.Create; temp.Delimiter := ' '; //分隔符 temp.DelimitedText := edit1.text; //需要分隔的字符串for i:=0 to temp.count - 1 do begin nums[i]:=StrToInt(temp.strings[i]); end;
var A array of string; i,pos:integer; begin pos :=0; str = 02 03 04 05 06 07 08 09 10 11 12 13';//要处理的字符,还可以放入TStringList中 for i:=0 to length(str) -1 do begin if str[i] == '' then begin continue; end; A[pos]:=str[i]; pos := pos +1; end; end;
ExtractStrings进行字符串分割
var
temp:TStrings;
nums:array of integer;
i:integer;
begin
temp:=TStringlist.Create;
ExtractStrings([' '],[],PChar(edit1.text),temp);
for i:=0 to temp.count - 1 do
SetLength(nums,temp.count);
begin
nums[i]:=StrToInt(temp.strings[i]);
end;
end;
看这里
var
temp:TStrings;
nums:array of integer;
i:integer;
begin
temp:=TStringlist.Create;
ExtractStrings([' '],[],PChar(edit1.text),temp);
SetLength(nums,temp.count);
for i:=0 to temp.count - 1 do
begin
nums[i]:=StrToInt(temp.strings[i]);
end;
end;
var
I: Integer;
A: array of Integer;
S0, S1: string;
begin
S0 := trim(Edit1.text) + ' ';
S1 := '';
SetLength(A, 0); for I := 1 to Length(S0) do
if S0[I] <> ' ' then S1 := S1 + S0[I]
else begin
SetLength(A, Length(A) + 1);
A[High(A)] := StrToIntDef(S1, 0);
S1 := '';
end;
end;
temp.Delimiter := ' '; //分隔符
temp.DelimitedText := edit1.text; //需要分隔的字符串for i:=0 to temp.count - 1 do
begin
nums[i]:=StrToInt(temp.strings[i]);
end;
A array of string;
i,pos:integer;
begin
pos :=0;
str = 02 03 04 05 06 07 08 09 10 11 12 13';//要处理的字符,还可以放入TStringList中
for i:=0 to length(str) -1 do
begin
if str[i] == '' then
begin
continue;
end;
A[pos]:=str[i];
pos := pos +1;
end;
end;