function IsNum(str: string): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(str) do
if not (str[i] in ['0'..'9']) then
begin
Result := False;
Break;
end;
end;
function IsNum(str: string): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(str) do
try
StrToInt(str[i]);
except
result := False;
Break;
end;
end;
end;
///////Begin Source
function IsNumber(mStr: string): Boolean; { 返回字符串是否是正确的数字表达 }
var
I: Real;
E: Integer;
begin
Val(mStr, I, E);
Result := E = 0;
E := Trunc(I);
end; { IsNumber }
function IsInteger(mStr: string): Boolean; { 返回字符串是否是正确的整数表达 }
var
I: Integer;
E: Integer;
begin
Val(mStr, I, E);
Result := E = 0;
E := Trunc(I);
end; { IsInteger }
function IsHex(mStr: string): Boolean; { 返回字符串是否是正确的十六进制表达 }
var
I: Integer;
begin
Result := true;
for I :=1 to Length(mStr) do
begin
//P := Copy(mStr,I,0);
if Pos(Upcase(mStr[I]),'0123456789ABCDEF')>0 then
Result :=Result and true
else
Result :=Result and false;
end;
end;
///////End Source
下面是Delphi中标准函数StrToInt的源代码,其中用到System单元Val内部函数,判定字符串是否整数最简单直接而且不会产生异常。
function StrToInt(const S: string): Integer;
var
E: Integer;
begin
Val(S, Result, E);
if E <> 0 then ConvertErrorFmt(@SInvalidInteger, [S]);
end;
function IsNum(str: string): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(str) do
if not (str[i] in ['0'..'9']) then
begin
Result := False;
Break;
end;
end;