本文整理自网络,侵删。
Delphi中,想判断一个字符串是否为数字,有好多方法,这里列出两种简单的方法:
1. 自定义函数
function isnum(str:string):boolean;
var
i:integer;
begin
for i:=1 to length(str) do
if not (str[i] in ['0'..'9']) then
begin
result:=false;
exit;
end;
result:=true;
end;
也可以作如下定义:
function IsDigit(S:String):Boolean;
var i,j:integer;
begin
Result:=True;
j:=0 ;
for i:=1 to length(s) do
begin
if not (s[i] in ['0'..'9','.'])then
Result:=False;
if s[i]='.' Then
j:=j+1;
end;
if j>1 then
Result:=False;
if (s[1]='.') or (s[length(s)]='.') then
Result:=False;
s:=copy(s,1, pos('.', S)-1);
j:=0;
for i:=1 to length(s) do
begin
if s[I]=’0’ then
j:=j+1;
end;
if j>1 then
Result:=False;
end;
2. 用获取例外的句柄
try
strtoint(ed1.Text);
except
showmessage('请输入合法匹配串!');
exit;
end;
相关阅读 >>
Delphi monthoftheyear、weekoftheyear、weekofthemonth、dayoftheyear … 相对时间
Delphi 字符串中末位是双字节字符的处理(避免最后一位为乱码)
更多相关阅读请进入《Delphi》频道 >>