如果有空格或者其他的字符呢? 介绍一个万无一失的方法://转换不成功则使用缺省值 function XStrToIntDef(const S:string;Def:Integer=0); var tmp:string; i:Integer; begin for i:=1 to Length(S) do if S[i] in ['0'..'9'] then tmp:=tmp+S[i]; Result:=StrToIntDef(tmp,Def); end;如果Delphi6及以下版本 //转换不成功则使用缺省值 function XStrToIntDef(const S:string;Def:Integer=0); var tmp:string; i:Integer; begin for i:=1 to Length(S) do if S[i] in ['0'..'9'] then tmp:=tmp+S[i]; if tmp='' then Result:=Def else Result:=StrToInt(tmp); end;
介绍一个万无一失的方法://转换不成功则使用缺省值
function XStrToIntDef(const S:string;Def:Integer=0);
var
tmp:string;
i:Integer;
begin
for i:=1 to Length(S) do
if S[i] in ['0'..'9'] then
tmp:=tmp+S[i];
Result:=StrToIntDef(tmp,Def);
end;如果Delphi6及以下版本
//转换不成功则使用缺省值
function XStrToIntDef(const S:string;Def:Integer=0);
var
tmp:string;
i:Integer;
begin
for i:=1 to Length(S) do
if S[i] in ['0'..'9'] then
tmp:=tmp+S[i];
if tmp='' then
Result:=Def
else
Result:=StrToInt(tmp);
end;