function StrToInt(const S: string): Integer;StrToInt converts the string S, which represents an integer-type number in either decimal or hexadecimal notation, into a number. If S does not represent a valid number, StrToInt raises an EConvertError exception.不是数字则会产生异常,自己处理下异常先!
1. StrToIntDef 函数可以将数据强制性转换成数字,而且不会出现出错提示。2. var I : Integer; S : string begin S := 'A123'; for I:=1 to Length(S) do if S[I] in ['0'..'9'] then showmessage('is Number') end
str:='asdg56fd';for i:=0 to length(str) do begin if (byte(str[i])<97) and (byte(str[i]<125)) then showmessage('该字符是一个数字') else showmessage('该字符是一个字符')
end;
请参考Val过程,Delhi6中,没有写在帮助中但是SysUtils单元中放出了这个函数. 具体参见StrToInt的源代码 uses Dialogs; var I, Code: Integer; begin { Get text from TEdit control } Val(Edit1.Text, I, Code); { Error during conversion to integer? } if Code <> 0 then MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0, mbOk); else Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));end;
function Isnumeric(str: string): Boolean; begin Result := Ture; try StrToFloat(str); except Result := False; end; end;
var i:integer; begin if tryStrToInt('1',i) then showmessage('正确') else showmessage('错误') end;
jinjazz(近身剪(N-P攻略)) 崇拜
if strtoint(i) then 数字 else 字符
function IsNUM(str: string): Boolean; begin Result := Ture; try StrToFloat(str); except Result := False; end; end;
用Val函数就可以,去看帮助有例子,或按大猛料得写法: function IsNumber(mStr: string): Boolean; { 返回字符串是否是正确的数字表达 } var I: Real; E: Integer; begin Val(mStr, I, E); Result := E = 0; end;
楼上的,那要是还有小数的呢,不就出错了。 我觉得 anbangs(菜鸟邦) 的可以考虑。
也简单啊.. var I : Integer; S : string begin S := 'A123'; for I:=1 to Length(S) do if S[I] in ['0'..'9','.'] then showmessage('is Number') end 多个'.'就行了..
var
I : Integer;
S : string
begin
S := 'A123';
for I:=1 to Length(S) do
if S[I] in ['0'..'9'] then showmessage('is Number')
end
begin
if (byte(str[i])<97) and (byte(str[i]<125)) then
showmessage('该字符是一个数字')
else
showmessage('该字符是一个字符')
end;
具体参见StrToInt的源代码
uses Dialogs;
var I, Code: Integer;
begin
{ Get text from TEdit control }
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0, mbOk);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));end;
begin
Result := Ture;
try
StrToFloat(str);
except
Result := False;
end;
end;
begin
if tryStrToInt('1',i) then showmessage('正确')
else
showmessage('错误')
end;
数字
else
字符
begin
Result := Ture;
try
StrToFloat(str);
except
Result := False;
end;
end;
function IsNumber(mStr: string): Boolean; { 返回字符串是否是正确的数字表达 }
var
I: Real;
E: Integer;
begin Val(mStr, I, E); Result := E = 0;
end;
我觉得 anbangs(菜鸟邦) 的可以考虑。
var
I : Integer;
S : string
begin
S := 'A123';
for I:=1 to Length(S) do
if S[I] in ['0'..'9','.'] then showmessage('is Number')
end
多个'.'就行了..