Delphi中判断一个字符串是否为数字


本文整理自网络,侵删。

 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 网址链接结尾自动补/

Delphi取得桌面工作区域的大小

Delphi monthoftheyear、weekoftheyear、weekofthemonth、dayoftheyear … 相对时间

Delphi什么是thttpclient?

Delphi vista以上系统的进程静音

Delphi 获取硬盘序列号(ide,sata,scsi)

Delphi 获取当前系统语言环境(en,fr,ja等)

Delphi获取系统电源状态的信息

Delphi 把流中的字符串转换为 utf 格式

Delphi 字符串中末位是双字节字符的处理(避免最后一位为乱码)

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...