How can I test if a given string contains a valid number?

Question:

How can I test if a given string contains a valid number?

Answer:

Use one of the string to number function inside a try except block, and test for an exception.

Example:

function IsStrANumber(NumStr : string) : bool;

begin

  result := true;

  try

  StrToInt(NumStr);

  except

  result := false;

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  if IsStrANumber(Edit1.Text) then

  ShowMessage(Edit1.Text) else

  ShowMessage('Not a Number');

end;