2009. január 5., hétfő

Does a string looks like an integer?


Problem/Question/Abstract:

Does a string looks like an integer?

Answer:

Use this function to determine, whether a given string represents an integer.


function IsInteger(TestThis: string): Boolean;
begin
  try
    StrToInt(TestThis);
  except
    on EConvertError do
      result := False;
  else
    result := True;
  end;
end;


Note: Due to the exception, the program is slow for non-numerical strings. If you expect non-numerical strings very often, you may use a construct basing on the Val() function and evaluate the error code.

Nincsenek megjegyzések:

Megjegyzés küldése