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 system 提供的编译期函数

Delphi 如何编写需要启动参数的Delphi程序

Delphi2010获取鼠标指向窗口的位置及鼠标在窗口内的相对位置坐标

Delphi读写ini文件加锁(独占)

Delphi中实现密码框“大写锁定打开”的提示

Delphi 设置richedit的行间距

Delphi2009 使用 png 图片

Delphi console 清屏代码

Delphi 如何判断clipboard剪切板中的内容的类型

Delphi 打开文件夹并定位到一个文件

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



打赏

取消

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

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

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

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

评论

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