Delphi isNumber

none picture none · Feb 3, 2011 · Viewed 81.5k times · Source

Is there a method in Delphi to check if a string is a number without raising an exception?

its for int parsing.

and an exception will raise if one use the

try
  StrToInt(s);
except
  //exception handling
end;

Answer

ba__friend picture ba__friend · Feb 3, 2011

function TryStrToInt(const S: string; out Value: Integer): Boolean;

TryStrToInt converts the string S, which represents an integer-type number in either decimal or hexadecimal notation, into a number, which is assigned to Value. If S does not represent a valid number, TryStrToInt returns false; otherwise TryStrToInt returns true.

To accept decimal but not hexadecimal values in the input string, you may use code like this:

function TryDecimalStrToInt( const S: string; out Value: Integer): Boolean;
begin
   result := ( pos( '$', S ) = 0 ) and TryStrToInt( S, Value );
end;