Главная страница | назад





Article #15884: How can I test if a given string contains a valid number?

 Question and Answer Database

FAQ884D.txt   How can I test if a given string contains a valid number?
Category   :Object Pascal
Platform    :All
Product    :All 32 bit  

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;

7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99