在Delphi中检查值是否为日期/数字的正确方法是什么

Re0*_*ess 6 delphi

在Delphi中检查值是否为日期/数字的正确方法是什么?

我知道其他语言有像isDate和isNaN这样的函数,但Delphi的等价物是什么?在我有这个的那一刻

function isNumeric(s1:string):boolean;
begin   
   // will throw exception if its not a number
  // there must be a better way to do this!!
  try
     StrTofloat(s1);
       result :=  TRUE ;
     except
       result := FALSE;
      end;
end;
Run Code Online (Sandbox Code Playgroud)

但抛出异常不能很好,而且每次调用代码时我都会看到异常对话,这会使调试变得困难.

Ala*_*lan 14

对于整数,您可以使用TryStrToInt检查和转换而不抛出异常:

function TryStrToInt(const s: string; out i : integer): boolean;
Run Code Online (Sandbox Code Playgroud)

我不完全确定浮点数有完整的等价物,所以你可能需要使用StrToFloat()并接受TFormatException的可能性.