如何判断一字符串是否为数字串?
如: '123.456000' 为数字,但'+123Edfd我12'为非字符串.
而'+123E-7',为数字串.
Delphi7帮助中:
对function StrToFloat(const S: string): Extended; overload;
中S的定义
S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored.若S为'+123Edfd我12',通过StrToFloat转换不成功,会引发EConvertError
异常.
能否通过这一点来判断S是否为数字串?(最好能简化为用if 判断)
本人一直以来对异常(C++,Delphi)都不明白,Delphi也刚学不久,望哪位大侠赐教!
如: '123.456000' 为数字,但'+123Edfd我12'为非字符串.
而'+123E-7',为数字串.
Delphi7帮助中:
对function StrToFloat(const S: string): Extended; overload;
中S的定义
S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored.若S为'+123Edfd我12',通过StrToFloat转换不成功,会引发EConvertError
异常.
能否通过这一点来判断S是否为数字串?(最好能简化为用if 判断)
本人一直以来对异常(C++,Delphi)都不明白,Delphi也刚学不久,望哪位大侠赐教!
NumReal: Real;Try
NumReal := StrToFloat(Str);
Except
ShowMessage(Str+' 不是一個有效的數值 ');
//Abort;
.....
End;
begin
Result := True;
try
n := StrToInt('你的字符串');
//类似的,其他类型数字用相应的函数
except
//如果不是数字字符串会出错,执行这里的代码
Result := False;
end;
end;