[fpc-pascal] Verify if string is a valid number

Johann Glaser Johann.Glaser at gmx.at
Sun Nov 23 17:23:32 CET 2008


Am Sonntag, den 23.11.2008, 14:18 -0200 schrieb Felipe Monteiro de
> Hello,
> I would like to verify if a string is a valid number, as in StrToInt
> will work when applied to it. The obvious way is making a try
> statement:
>       try
>         StrToInt(TokenStr);
>         AdicionarToken(tNumeroLiteral, TokenStr);
>       except
>         AdicionarToken(tId, TokenStr);
>       end;
> but I don't like this solution, because a lot of exceptions will be
> caused by normal execution of my program, which will disturb debugging
> it ...

Always avoid exceptions if possible, since they are very costly in terms
CPU time. Do your error handling with exceptions, but not routine tests.
This would be misuse of exceptions.

For your problem just use the original Pascal procedure
  Val(St:String;Var Value,Position:Integer)
which simply returns the position of an illegal character or 0 if the
string was a legal number.


More information about the fpc-pascal mailing list